|
C#可以创建开放委托和闭和委托,在默认情况下:
开放委托:为静态方法创建
闭合委托:为实例方法创建
偶然发现,也能为实例方法创建出开放委托:
- /// <summary>
- /// Creates an open delegate for the given (dynamic)method.
- /// </summary>
- public static Delegate CreateDelegate(MethodInfo methodInfo, Type delegateType) {
- Contract.RequiresNotNull(delegateType, "delegateType");
- Contract.RequiresNotNull(methodInfo, "methodInfo");
- DynamicMethod dm = methodInfo as DynamicMethod;
- if (dm != null) {
- return dm.CreateDelegate(delegateType);
- } else {
- return Delegate.CreateDelegate(delegateType, methodInfo);
- }
- }
- /// <summary>
- /// Creates a closed delegate for the given (dynamic)method.
- /// </summary>
- public static Delegate CreateDelegate(MethodInfo methodInfo, Type delegateType, object target) {
- Contract.RequiresNotNull(methodInfo, "methodInfo");
- Contract.RequiresNotNull(delegateType, "delegateType");
- DynamicMethod dm = methodInfo as DynamicMethod;
- if (dm != null) {
- return dm.CreateDelegate(delegateType, target);
- } else {
- return Delegate.CreateDelegate(delegateType, target, methodInfo);
- }
- }
复制代码
https://ironscheme.svn.codeplex.com/svn/IronScheme/Microsoft.Scripting/Utils/ReflectionUtils.cs
|
|