vendredi 25 mars 2016

C# check if generic type has attribute by string and assign to it

Let's say, I have a class.

class A {
  string X {get; set;}
  string Y {get; set;}
}

and in some method I want to ask, if a generic class (in my case A) has a by string specified argument, and if so, assign to it a value;

class CreateGenerics<T> where T:new()
{
  public List<T> create(string attr, string[] attrValues) 
  {
    //I want to check if T has attribute attr (let's say "X")

    List<T> ret = new List<T>();
    foreach(string value in attrValues) {
      T t = new T();
      //set 'attr' attribute of T to value of 'value'
      ret.Add(t);
    }
    return ret;
  }
}





Aucun commentaire:

Enregistrer un commentaire