| 

.NET C# Java Javascript Exception

2
Hallo Zusammen,

ich möchte alle Properties einer Classe (Instanz), die mit einem CustomAttribute markiert sind in eine Liste packen.

ich glaube der Code erklärt ziemlich gut was ich erreichen will.

[AttributeUsage(AttributeTargets.Property)]
public class FieldDescriptionAttribute : Attribute
{
public string DisplayName { get; set; }
public FieldDescriptionAttribute(string displayName)
{
DisplayName = displayName;
}
}

public class IntegerField
{
public string Name { get; set;}
public Func<int> Getter { get; set; }
public Action<int> Setter { get; set; }

public T Value
{
get { return Getter(); }
set { Setter(value); }
}

public IntegerField(Func<int> getter, Action<int> setter, string name)
{
Name = name;
Getter = getter;
Setter = setter;
}
}
class Foo
{
public ObservableCollection<IntegerField> Fields { get; set; }

[FieldDescriptionAttribute("Blub")]
public int Something { get; set; }

public void GetFields()
{
var type = this.GetType();
var properties = type.GetProperties( BindingFlags.Instance | BindingFlags.Public);

foreach (var pi in properties)
{
var ca = pi.GetCustomAttributes(typeof(FieldDescriptionAttribute), true);
if (ca != null && ca.Length > 0)
{
var attribute = ca.FirstOrDefault() as FieldDescriptionAttribute;

if (pi.PropertyType == typeof(int))
{
var setter = // Was kommt hier rein?
var getter = // manuel wäre hier sowas: new Action<int>(() => Something);
// new Func<int>(value => Something = value);
var fvm = new IntegerField(getter, setter, attribute.DisplayName);
Fields.Add(fmv);
}
}
}
}

}
News:
05.04.2012
lj_1900 45 1 4
2 Antworten
3
Hallo,

du bist eigentlich schon fast am Ziel:
public class IntegerField
{
public string Name { get; set; }
public Func<int> Getter { get; set; }
public Action<int> Setter { get; set; }

public int Value
{
get { return Getter(); }
set { Setter(value); }
}

public IntegerField(Func<int> getter, Action<int> setter, string name)
{
Name = name;
Getter = getter;
Setter = setter;
}
}

class Foo
{
public ObservableCollection<IntegerField> Fields { get; internal set; }

public Foo()
{
this.Fields = new ObservableCollection<IntegerField>();
}

[FieldDescriptionAttribute("Blub")]
public int Something { get; set; }

public void GetFields()
{
var type = this.GetType();
var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

foreach (var pi in properties)
{
var ca = pi.GetCustomAttributes(typeof(FieldDescriptionAttribute), true);
if (ca != null && ca.Length > 0)
{
var attribute = ca.FirstOrDefault() as FieldDescriptionAttribute;

if (pi.PropertyType == typeof(int))
{
var setter = new Action<int>(value => pi.SetValue(this, value, null));
var getter = new Func<int>(() => (int)pi.GetValue(this, null));
var fvm = new IntegerField(getter, setter, attribute.DisplayName);
this.Fields.Add(fvm);
}
}
}
}
}

Test
Foo foo = new Foo();
foo.GetFields();
foo.Something = 23;

foreach (var fvm in foo.Fields)
{
Console.WriteLine(string.Format("{0}", fvm.Value));
}

foreach (var fvm in foo.Fields)
{
fvm.Value = 10;
}

foreach (var fvm in foo.Fields)
{
Console.WriteLine(string.Format("{0}", fvm.Value));
}


Ausgabe:
23
10
05.04.2012
PinBack 687 1 8
Danke schön.
hätte nicht gedacht, dass es so einfach ist :)
lj_1900 05.04.2012
0
kleinen Bug entdeckt:

if (pi.PropertyType == typeof(int))
{
var p = pi; // <--- Wichtig!!!
var setter = new Action<int>(value => p.SetValue(this, value, null));
var getter = new Func<int>(() => (int)p.GetValue(this, null));
var fvm = new IntegerField(getter, setter, attribute.DisplayName);
this.Fields.Add(fvm);
}
13.04.2012
lj_1900 45 1 4

Stelle deine .net-Frage jetzt!
TOP TECHNOLOGIES CONSULTING GmbH