lundi 14 décembre 2015

C# Generic parameter from a string variable

I have two classes, Customer and Country. Customer has a property called HomeCountry, which i have decorated with a custom attribute called "Lookup" and takes a string parameter "Country". The purpose is, when I am using the Customer class, the item in HomeCountry must exist in the Country class (which happens to be a list).

I am using reflection to iterate the Customer class, it finds the attribute and i want it to check the value in the list of country items. So far I have:

foreach (PropertyInfo _pi in object.GetType().GetProperties()) {
  IEnumerable<Attribute> _attrs = _pi.GetCustomAttributes();
  foreach (Attribute _a in _attrs) {
    Object obj = Activator.CreateInstance(_type, null);
    // what goes here?
  }
}

I have a method:

public T Populate<T>(params string[] _parameters)

I think i want to do

List<obj> v = populate<obj>();

or

List<typeof(obj)> v = populate<typeof(obj)>();

but obviously nothing works! Can anybody help me ?

Thanks





Aucun commentaire:

Enregistrer un commentaire