Using Impromptu-Interface to obtain the type of a property

786 views Asked by At

I've got a complex solution where part of the problem is model binding from a HTML form to a series of database backed and relatively complex Entity Framework DbSets.

The thing is, we have an EF defined domain model that encapsulates everything we'd need to know about the data we're capturing; but the admins of the project want to be able to make a questionnaire-like form, that allows them to choose any of the members of this domain.

Anyway, that's not the problem as such, as it largely works, at least it works very well for simple members, strings, dates, bools and so on. The tricky part was managing members that have multiple fields, such as an Address object.

A solution has been to use Reflection to set the value of the domain that we receive from the form post, but of course that has its overhead and I'm driven to find a nicer way of doing things; In my research I found out about the 'Impromptu interface' project which promises a lot of speed increase over Reflection, but I have one simple problem.

It's all well and good to Get and Set properties:

var val = Impromptu.InvokeGet(domain, "fieldName");
Impromptu.InvokeSet(domain, "fieldName", value);

But what I need to do is to find the Type of the property. So far I can still only see how to do that with Reflection:

PropertyInfo pi = domain.GetType().GetProperty("Name", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
if (pi.GetValue(domain) is IMyInterface)
{
   // ? profit
}

So: Is it possible to do this with Impromptu? I need to cast the property to my Interface as it has members that convert html form posts into my EF objects.

1

There are 1 answers

4
jbtule On BEST ANSWER

The general question you ask, can I use ImpromptuInterface to query property types, the answer is no, the DLR doesn't have the function, reflection is it.

However, the example you give using reflection isn't testing the property type, it's testing the runtime type of the value so that would still work with Impromptu without reflection.

var val = Impromptu.InvokeGet(domain, "fieldName");
if(val is IMyInterface){
    // ? profit
}

Also if you only want properties look at FastMember. It choose the fastest access mechanism based on the type of object.