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();

谢谢

4

1 回答 1

3

这种方法能做到吗?试试看。

然而:

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

应该替换为

TEntity newEntity = new TEntity();

在为参数添加new()通用约束之后。这将添加编译时检查以确保实体具有有效的无参数构造函数。IE:

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

并且您不需要ref在第一种方法中,假设您的所有实体都是class类型。

于 2013-01-02T12:10:13.013 回答