public class Foo
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public int Prop3 { get; set; }
}
class Program
{
static void Main(string[] args)
{
var foo = new Foo();
// Use reflection to get all string properties
// that have getters and setters
var properties = from p in typeof(Foo).GetProperties()
where p.PropertyType == typeof(string) &&
p.CanRead &&
p.CanWrite
select p;
foreach (var property in properties)
{
var value = (string)property.GetValue(foo, null);
if (value == null)
{
property.SetValue(foo, string.Empty, null);
}
}
// at this stage foo should no longer have null string properties
}
}
स्रोत
2010-11-27 10:03:30
कि एक काफी अजीब बात की तरह लगता है क्या कर रही किया जाना है। मुझे यह जानकर उत्सुकता है कि आप इसके साथ क्या हासिल करने की कोशिश कर रहे हैं? – andynormancx
एक तरफ नोट पर, हो सकता है कि आप अपने नल स्ट्रिंग को "स्ट्रिंग.एक्टी" के बजाय "" पर सेट करना चाहें। असली दुनिया का प्रभाव नगण्य है, लेकिन कुशल कोड के लिए पूर्व एक नई वस्तु नहीं बनाता है। – Cranialsurge
इसके अलावा, मैं andynormancx से सहमत हूं .... आपका उद्देश्य क्या है? – Cranialsurge