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、NameとValueの2つのプロパティが含まれています。

したがって、ジェネリック型を取得し、新しい型を初期化してそのプロパティに値を入力する必要があるメソッドがあります。このメソッドはそれを実行しますか?

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

}

これはNonGenericsのように機能しますか?

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()パラメータの一般的な制約を追加した後。これにより、コンパイル時のチェックが追加され、エンティティに有効なパラメーターなしのコンストラクターがあることを確認します。すなわち:

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

また、すべてのエンティティが型refであると仮定すると、最初のメソッドでは必要ありません。class

于 2013-01-02T12:10:13.013 に答える