`
RednaxelaFX
  • 浏览: 3015900 次
  • 性别: Icon_minigender_1
  • 来自: 海外
社区版块
存档分类
最新评论

通过get或set方法的MethodInfo获得相应的PropertyInfo的方式

    博客分类:
  • DLR
阅读更多
在IronPython 46307的MemberExpression.cs里,可以看到DLR是如何获取一个get或者set方法对应的属性的PropertyInfo的:
/// <summary>
/// Creates a <see cref="MemberExpression"/> accessing a property.
/// </summary>
/// <param name="expression">The containing object of the property.  This can be null for static properties.</param>
/// <param name="propertyAccessor">An accessor method of the property to be accessed.</param>
/// <returns>The created <see cref="MemberExpression"/>.</returns>
public static MemberExpression Property(Expression expression, MethodInfo propertyAccessor) {
    ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
    ValidateMethodInfo(propertyAccessor);
    return Property(expression, GetProperty(propertyAccessor));
}

private static PropertyInfo GetProperty(MethodInfo mi) {
    Type type = mi.DeclaringType;
    BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic;
    flags |= (mi.IsStatic) ? BindingFlags.Static : BindingFlags.Instance;
    PropertyInfo[] props = type.GetProperties(flags);
    foreach (PropertyInfo pi in props) {
        if (pi.CanRead && CheckMethod(mi, pi.GetGetMethod(true))) {
            return pi;
        }
        if (pi.CanWrite && CheckMethod(mi, pi.GetSetMethod(true))) {
            return pi;
        }
    }
    throw Error.MethodNotPropertyAccessor(mi.DeclaringType, mi.Name);
}

private static bool CheckMethod(MethodInfo method, MethodInfo propertyMethod) {
    if (method == propertyMethod) {
        return true;
    }
    // If the type is an interface then the handle for the method got by the compiler will not be the 
    // same as that returned by reflection.
    // Check for this condition and try and get the method from reflection.
    Type type = method.DeclaringType;
    if (type.IsInterface && method.Name == propertyMethod.Name && type.GetMethod(method.Name) == propertyMethod) {
        return true;
    }
    return false;
}

这代码太丑了,居然要自己遍历类型的所有属性,线性的一个个找 = =
.NET Framework的标准库怎么就不提供标准的方法来查询一个方法是否是某个属性的get或者set方法呢,属性明明不只是C#的概念,也是CLI的概念啊。看来类似的功能得加到自己的utils里去,不然每次都这么搞太麻烦了。

DLR里有很多扩展方法都值得借鉴,因为光靠.NET标准库提供的方法很多时候还是会重复不少代码的。像是一些对数组的扩展方法就挺不错的。
分享到:
评论

相关推荐

    Methodinfo属性调用静态方法.pdf

    public string Name { get; set; } public bool Gender; public string ShowLove(string targetName) { return targetName + ", i love u .msg from " + this.Name; } public Student(string name) { ...

    01:详解C#中的反射.pdf

    可以调用这些成员,其方式是调用Type的InvokeMember()方法,或者调用MethodInfo, PropertyInfo和其他类的Invoke()方法。 查看类中的构造方法: NewClassw nc = new NewClassw(); Type t = nc.GetType(); ...

    反射5大对象

    对象为Assembly 可以获取到程序集文件 ... 对象MethodInfo 可以获得类的方法的对象 对象PropertyInfo可以获得类的字段的对象 对象FieldInfo 可以获得类的属性的对象 对象EventInfo 可以获得类的事件的对象

    C#反射机制.doc

    元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等。 System.reflection命名空间包含的几个类,允许你反射(解析)这些...

    C# IsDefined的问题

    在.NET 4.0(当然也包括4.0以前的版本)下,用反射判断某个方法是否运用了自定义Attribute时,可以通过调用MethodInfo的IsDefined()方法进行确认。...如果获得MethodInfo的方式是通过加载程序集,然后利

    c#反射调用方法示例

    在.Net 中, 一旦获得了Type对象,就可以使用GetMethods()方法获取此类型支持的方法列表;该方法的两种形式: MethodInfo [] GetMethods() MethodInfo [] GetMethods(BindingFlags bindingflas) :它的参数带有一些...

    C#中事件的动态调用实现方法

    一般来说,传统的思路是,通过Reflection.EventInfo获得事件的信息,然后使用GetRaiseMethod方法获得事件被触发后调用的方法,再使用MethodInfo.Invoke来调用以实现事件的动态调用。 但是很不幸的,Reflection....

    Emit实现AOP示例

    var propMethod = ctxType.GetMethod("set_ParameterArgs"); il.Emit(OpCodes.Ldloc, ctx); il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Call, propMethod); int m = attrs.Length; LocalBuilder[] lbs = new...

    FastDelegate.Net:.Net 缓慢的 Delegate.DynamicInvoke 的替代品

    它适用于任何方法、实例或静态、无效返回或其他方式。 所有方法都以相同的方式绑定: using FastDelegate.Net; System.Reflection.MethodInfo myMethodInfo; // fill in myMethodInfo // ... Func&lt;Object, ...

    关于C#基础知识回顾–反射(二)

    使用反射调用方法:一旦知道一个类型所支持的方法,就可以对方法进行调用。调用时,需使用包含在MethodInfo中的Invoke()方法。...要调用某个方法,只需在一个MethodInfo实例上调用Invoke(),该实例通过调用Ge

    Weakly:弱是一些有用的弱引用类型的集合

    可通过NuGet获得弱点: PM&gt;安装软件包 内容 建筑商 为以下内容创建编译的表达式: Builder.DynamicDelegate创建MethodInfo.Invoke的编译版本 Builder.OpenAction创建开放的委托 Builder.OpenFunc创建开放委托 ...

    关于【C#反射】文章中涉及到的内容示例项目工程

    关于【C#反射】文章中涉及到的内容示例项目工程,包含如下功能: ①获取反射类型的三种方式; ②FieldInfo 通过反射获取对象的成员字段示例;...③MethodInfo 通过反射获取某个类的方法且执行示例;

    PowerShellArsenal:专用于逆向工程的PowerShell模块

    Get-ILDisassembly 反汇编从MethodInfo对象传入的原始MSIL字节数组,其方式与Ildasm相似。恶意软件分析执行恶意软件分析时的有用工具。New-FunctionDelegate 提供X86或X86_64函数的可执行包装。Invoke-LoadLibrary ...

    c# 反射实例

    MethodInfo method = type.GetMethod(methodName); object[] parameters = new object[] { param }; // 创建实例;调用方法(传递参数,获取返回值) object instance = assembly.CreateInstance(type.ToString()...

    Extremely fast reflection - MakeGenericMethod (using MSIL) - 2.5x faster!

    Fastest MethodInfo.MakeGenericMethod ever! It's written using MSIL. 2.5x faster than the original method.

    Irelia:基于netty的api网关

    irelia-spi-core: spi 核心模块,提供 spi 的基本方法,如提取 IreliaBean、methodInfo 等。 irelia-spi-dubbo: 为 dubbo 项目提供的快速接入包。 Irelia-upstream: upstream 核心模块,提供了 upstream 的核心接口...

    JVM基础.doc

    • ClassFile中FieldInfo和MethodInfo介绍 • 类型描述Descriptor介绍 • ClassFile中的Attribute介绍 • JVM指令介绍,获得ClassLoader的途径,CAS指令由硬件提供 • 并发程序设计实现的基础 • 486之后并不需要锁...

    autocad objectarx acmgd.dll 导出的.net类资料清单

    格式如下: 类:Autodesk.AutoCAD.ApplicationServices.DocumentLockModeWillChangeEventArgs:System.EventArgs ... Method AS System.Reflection.MethodInfo 可读不可写 Target AS System.Object 可读不可写

    WebService.cs

    动态调用WebService的方法类,可以不用在VS本地引用,直接动态调用即可。 样例如下: /// /// 实例化WebServices /// /// &lt;param name="url"&gt;WebServices地址 /// 调用的方法 /// 把webservices里需要的参数...

    jaxrs-request-matching:eclipse-ee4jjaxrs-api#904的实验

    net.example.jaxrs 这些建立可用资源类和方法的注册表: PathInfo ResourceInfo MethodInfo Introspector 匹配算法的实现: RequestMatching RequestMatchingSpec RequestMatchingEnhanced MatchInfo – ...

Global site tag (gtag.js) - Google Analytics