简体   繁体   English

强制转换通用类型然后使用Ref

[英]Casting Generic Type then Using Ref

I need some clarity for my problem. 我需要澄清我的问题。

I have a method which does this: 我有一个方法可以做到这一点:

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

The PropertyHelper just contains two properties, Name and Value. PropertyHelper仅包含两个属性,名称和值。

So, I have a method that takes an Generic Type and then needs to initialise a new one and fill its properties with values, will this method do that: 因此,我有一个采用通用类型,然后需要初始化一个新类型并用值填充其属性的方法,该方法可以做到这一点:

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

}

Will this work like Non Generics would do: 像Non Generics这样的作品能做到吗?

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

}

So Effectively is: 因此有效是:

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

Equivalent to this: 等效于:

MyEntity newEntity = new MyEntity();

Thanks 谢谢

Will that method do the thing? 那方法能做得到吗? Try it and see. 试试看。

However: 然而:

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

should be replaced with 应该替换为

TEntity newEntity = new TEntity();

after adding the new() generic constraint for the parameter. 在为参数添加new()通用约束之后。 This will add compile time checks to ensure that the entity has a valid parameterless constructor. 这将添加编译时间检查,以确保实体具有有效的无参数构造函数。 Ie: 即:

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

And you don't need ref in your first method, assuming all your entities are class types. 并且,假设您所有的实体都是class类型,则不需要在第一个方法中使用ref

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM