2

pocoクラスの属性に基づいて流暢なマッピングを動的に実行しており、.Propertyケースは正常に機能していますが、.Ignore()メソッドを実行しようとすると爆発します。

private void AddEntities(DbModelBuilder modelBuilder)
{
    var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");
    foreach (var entityType in EntityBaseTypes)
    {
        dynamic entityConfiguration = entityMethod.MakeGenericMethod(entityType).Invoke(modelBuilder, new object[] { });
        foreach (PropertyInfo propertyInfo in entityType.GetProperties().Where(x => x.CanWrite))
        {

            foreach (var attribute in propertyInfo.GetCustomAttributes())
            {
                LambdaExpression propertyLambda = PropertyGetLambda(entityType, propertyInfo.Name, typeof(string));
                string attributeName = attribute.GetType().Name;
                if (attributeName == "NotMappedAttribute")
                {
                    MethodInfo ignoreMethod = (MethodInfo)entityConfiguration.GetType().GetMethod("Ignore");                            
//BLOWS UP HERE: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true
                    ignoreMethod.Invoke(entityConfiguration, new[] { propertyLambda });

                }
                else if (attributeName == "ColumnAttribute")
                {
                    dynamic column = attribute;
                    var propertyMethod = entityConfiguration.GetType().GetMethod("Property", new Type[] { propertyLambda.GetType() });
                    var entityProperty = (PrimitivePropertyConfiguration)propertyMethod.Invoke(entityConfiguration, new[] { propertyLambda });
//WORKS FINE:
                    entityProperty.HasColumnName(column.Name);
                }
            }
        }

    }
}

.Ignore()メソッドのパラメーターに必要な型は違うと思います。これは私が呼び出そうとしているメソッドです:.Ignore()

public void Ignore<TProperty>(
    Expression<Func<TStructuralType, TProperty>> propertyExpression
)
4

1 に答える 1

1

私はあなたが使用する方法のMemberInfoためにを取得する必要があると思います:Ignore

MethodInfo ignoreMethod = typeof(StructuralTypeConfiguration<>)
    .MakeGenericType(entityType)
    .GetMethod("Ignore")
    .MakeGenericMethod(propertyInfo.PropertyType);

Ignoreはのプロパティタイプに対応するジェネリックパラメータを持つメソッドであるExpressionため、呼び出す前にプロパティタイプを指定する必要があります。

于 2013-02-12T23:20:44.160 に答える