简体   繁体   English

使用即兴接口来获取属性的类型

[英]Using Impromptu-Interface to obtain the type of a property

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. 我有一个复杂的解决方案,其中部分问题是从HTML表单到一系列数据库支持且相对复杂的Entity Framework DbSet的模型绑定。

The thing is, we have an EF defined domain model that encapsulates everything we'd need to know about the data we're capturing; 关键是,我们有一个EF定义的域模型,该模型封装了我们需要了解的有关捕获数据的所有信息; 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. 棘手的部分是管理具有多个字段的成员,例如Address对象。

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; 一种解决方案是使用Reflection设置从表单发布中接收到的域的值,但是当然这会产生开销,因此我被迫寻找一种更好的处理方式。 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. 在我的研究中,我发现了“ Impromptu接口”项目,该项目有望比Reflection提速很多,但是我有一个简单的问题。

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. 我需要将该属性转换为我的Interface,因为它具有将html表单帖子转换为我的EF对象的成员。

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. 您问的一般问题,我可以使用ImpromptuInterface查询属性类型,答案是否定的,DLR没有该功能,反射就是它。

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. 但是,您使用反射给出的示例不是在测试属性类型,而是在测试值的运行时类型,因此仍然可以在不使用反射的情况下与Impromptu一起使用。

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

Also if you only want properties look at FastMember . 另外,如果只希望属性,请查看FastMember It choose the fastest access mechanism based on the type of object. 它根据对象的类型选择最快的访问机制。

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

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