portjm 0 Newbie Poster

Im attempting to set the column name on a boolean property using reflection. This works for the standard String, Int etc but there is no Property method that takes a Expression<Func<Object,Boolean>>

Essentially its this modelBuilder.Entity<Object>().Property(g => g.boolDeleted).HasColumnName("BooleanColumn");
And the line above works fine. But when calling the method by reflection it gets an ArugmentTypeException

ParameterExpression parameter = Expression.Parameter(entityObject, "t");
var expressionProperty = Expression.Property(parameter, propertyInfo.Name);

const string methodSignature = "System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate]" + "(System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])";
var generatedLambdaMethod = typeof(Expression).GetMethods()
.Single(mi => mi.ToString() == methodSignature);

var func = typeof(Func<,>).MakeGenericType(new[] { entityObject, typeof(Boolean) });

var genericLambda = generatedLambdaMethod.MakeGenericMethod(func);

var generatedLambdaInvoked = genericLambda.Invoke(null,
new object[] { expressionProperty, new[] { parameter } });

var method = modelBuilder.GetType().GetMethod("Entity");

var genericMethod = method.MakeGenericMethod(new[] { entityObject })
    .Invoke(modelBuilder, new object[] { });

var propertyMethod = genericMethod.GetType()
    .GetMethods()
    .Where(m => m.Name == "Property" && m.GetParameters().Where(p => p.ParameterType == generatedLambdaInvoked .GetType()).Any() == true)
    .Single();

var propInvoked = propertyMethod.Invoke(genericMethod, new[] { generatedLambdaInvoked });                       


var hasColumnNameInvoked = propInvoked.GetType()
    .GetMethods()
    .Where(m => m.Name == "HasColumnName")
    .First()
    .Invoke(propInvoked, new object[] { "BooleanColumn" });
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.