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
}
}
sehen, dass wie eine ziemlich seltsame Sache klingt zu tun. Ich bin neugierig zu wissen, was Sie damit erreichen wollen? – andynormancx
Als Randnotiz möchten Sie vielleicht Ihre Nullstrings auf "String.Empty" anstatt auf "" einstellen. Der Einfluss der realen Welt ist vernachlässigbar, aber aus Gründen des effizienten Codes erzeugt der erstere kein neues Objekt. – Cranialsurge
Auch stimme ich mit andynormancx .... was ist Ihr Ziel hier? – Cranialsurge