2013-01-02 43 views
0

我需要一些清晰的問題。鑄造泛型然後使用參考

我有做到這一點的方法:

public static void SetEntityValue<TEntity>(ref TEntity entityToTransform, PropertyHelper entityProperty) 
{ 
    // create type from entity 
    Type t = entityToTransform.GetType(); 
    // get the property to set 
    var prop = t.GetProperty(entityProperty.Name); 
    // set the property value to the one parsed 
    prop.SetValue(entityToTransform, entityProperty.Value, null); 
} 

PropertyHelper只包含兩個屬性,名稱和值。

所以,我必須接受一個泛型類型的方法,然後需要初始化一個新的,並用填充值的屬性,將這種方法做:

TEntity ReadIntoEntity<TEntity>(TEntity entity, XElement node) 
{ 
    if (!node.HasElements) 
     throw new IllFormedDocumentException("Entity found but contains no properties"); 

    var xmlProps = node.Elements(); 

    Type t1 = entity.GetType(); 

    // the line which initialises a new TEntity same as string myString = new string(); 
    TEntity newEntity = Activator.CreateInstance<TEntity>(); 

    var props = t1.GetProperties(); 
    var readableProps = props.Select(x => new PropertyHelper(GenericHelper.GetEntityProperty(x), GenericHelper.GetEntityValueAsObject<TEntity>(entity, x))); 

    List<string> foundAProp = new List<string>(); 

    foreach (var el in xmlProps) 
    { 
     // iterate through all xml elements 

     foreach (var prop in readableProps) 
     { 
      // check the prop exists in the xml set 

      // We found a prop that exists! 
      if (el.Name.ToString() == prop.Name.ToString()) 
      { 
       foundAProp.Add(prop.Name.ToString()); 

       GenericHelper.SetEntityValue<TEntity>(ref newEntity, prop); 
      } 
     } 
    } 

} 

會喜歡非泛型這項工作會這樣做:

MyEntity ReadIntoEntity(XElement node) 
{ 
    if (!node.HasElements) 
     throw new IllFormedDocumentException("Entity found but contains no properties"); 

    var xmlProps = node.Elements(); 

    MyEntity newEntity = new MyEntity(); 

    var props = typeof(MyEntity).GetProperties(); 
    var readableProps = props.Select(x => new PropertyHelper(GenericHelper.GetEntityProperty(x), GenericHelper.GetEntityValueAsObject<TEntity>(entity, x))); 

    List<string> foundAProp = new List<string>(); 

    foreach (var el in xmlProps) 
    { 
     // iterate through all xml elements 

     foreach (var prop in readableProps) 
     { 
      // check the prop exists in the xml set 

      // We found a prop that exists! 
      if (el.Name.ToString() == prop.Name.ToString()) 
      { 
       foundAProp.Add(prop.Name.ToString()); 

       GenericHelper.SetEntityValue<MyEntity>(ref newEntity, prop); 
      } 
     } 
    } 

} 

所以實際上是:

TEntity newEntity = Activator.CreateInstance<TEntity>(); 

等效於此:

MyEntity newEntity = new MyEntity(); 

感謝

回答

3

將這種方法做的事情嗎?試試看看。

但是:

TEntity newEntity = Activator.CreateInstance<TEntity>(); 

TEntity newEntity = new TEntity(); 

加入new()通用約束的參數後進行更換。這將添加編譯時間檢查以確保實體具有有效的無參數構造函數。即:

TEntity ReadIntoEntity<TEntity>(TEntity entity, XElement node) 
    where TEntity : class, new() 
{ 
    // ... 

而且你不需要ref在你的第一種方法,假設所有的實體class類型。

+0

那麼where子句是什麼意思?然後:class,new()是什麼意思? –

+0

這是一個通用約束,它指定在調用方法時,「TEntity」類型可能只是帶有無參數構造函數的引用('class')類型。閱讀http://msdn.microsoft.com/en-us/library/d5x73970.aspx –

+0

關於const的所有內容你是什麼意思「你怎麼稱呼它」?你把它稱之爲任何你已經無數次做過的其他方法。 –