为什么我不能叫Delegate.CreateDelegate在我的便携式类库? [英] Why can't I call Delegate.CreateDelegate in my Portable Class Library?

查看:163
本文介绍了为什么我不能叫Delegate.CreateDelegate在我的便携式类库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:我想从我的便携式类库针对.NET 4.5中调用 Delegate.CreateDelegate 的Windows Phone 8和Windows 8商店应用程序,但我的代码不能编译。编译器说,它无法找到关于代表键入

I have the following problem: I want to call Delegate.CreateDelegate from within my Portable Class Library targeting .NET 4.5, Windows Phone 8 and Windows 8 Store Apps, but my code does not compile. The compiler says that it cannot find the method on the Delegate type.

有趣的是,例如方法PRISM的库由微软可以从便携式类库称之为Delegate.CreateDelegate。它这样做是在 DelegateReference 类。棱镜便携式类库目标.NET 4.0,Windows 8的应用程序商店的Windows Phone 8和Silverlight 5(正是如此更严格的设置)。

The funny thing is that e.g. the PRISM library by Microsoft can call 'Delegate.CreateDelegate' from a Portable Class Library. It does so in the DelegateReference class. The PRISM portable class library targets .NET 4.0, Windows 8 Store Apps, Windows Phone 8 and Silverlight 5 (and thusly an even more restrictive set).

中的代码,做不能编译如下所示:

The code that does not compile looks like this:

public class MyClass
{
    public void MyMethod<T>(EventHandler handler)
    {
        var @delegate = Delegate.CreateDelegate(typeof (OpenEventHandler<T>), null, handler.GetMethodInfo());
    }
}

public delegate void OpenEventHandler<in T>(T target, object sender, EventArgs arguments);



一个例子可以在这里下载:的https://dl.dropboxusercontent.com/u/14810011/PortableClassLibraryReferenceProblem.zip

它包含了我的图书馆项目只包含 DelegateReference 类及其接口PRISM PubSubEvents项目的一个非常精简版本。后者的完整的源代码可以在这里找到: http://prismwindowsruntime.codeplex.com/SourceControl/latest

It contains my library project and a very stripped version of the PRISM PubSubEvents project containing only the DelegateReference class and its interface. The complete source code of the latter can be found here: http://prismwindowsruntime.codeplex.com/SourceControl/latest

我能做些什么来使用所有代表成员? !预先感谢您的帮助。

What can I do to use all Delegate members? Thank you in advance for your help!

修改亨克Holterman的回答后:

EDIT after Henk Holterman's answer:

GetMethodInfo()是被PCL子集支持的扩展方法。无论如何,这是不相关的,我不能称之为问题 Delegate.CreateDelegate 同时PRISM的PCL项目都可以。

GetMethodInfo() is an extension method that is supported by the PCL subset. Anyway, that is not related to the problem that I cannot call Delegate.CreateDelegate while PRISM's PCL project can.

编辑2 汉斯Passants后评论:

EDIT 2 after Hans Passants comment:

我只是打了一圈,发现的是,当我启动的Silverlight 5的移植库的目标,然后 Delegate.CreateDelegate 是确实可以访问(和GetMethodInfo扩展方法不再是)。为 Delegate.CreateDelegate 那么也许映射到另一个API为Windows 8商店和Phone应用内部?这是我能想到的是如何这个方法会仅仅因为我说的Silverlight 5作为一个有效的目标突然访问的唯一途径。

I just played around and found out that when I activate Silverlight 5 as a target of the portable library then Delegate.CreateDelegate is indeed accessible (and the GetMethodInfo Extension Method no longer is). Is Delegate.CreateDelegate then maybe mapped to another API for Windows 8 Store and Phone apps internally? It's the only way I could think of how this method would be suddenly accessible just because I added Silverlight 5 as a valid target.

(您可以通过右键点击复制本对MyPortableClassLibrary项目,然后单击属性,在库选项卡中单击更改选择由便携式图书馆有针对性的框架。)

(You can reproduce this by right-clicking on the "MyPortableClassLibrary" project, click "Properties" and in the "Library" tab click change to select the frameworks that are targeted by the portable library.)

此外,今天早些时候,我创建了一个Windows应用商店的应用项目,并看到有关​​于在代表类没有定义 createDelegate方法方法。.NET为Windows运行时

Also, earlier today, I created an Windows Store App project and saw that there was no CreateDelegate method defined on the Delegate class in .NET for Windows Runtime.

在我的实际项目中,我不希望因为我用的目标的Silverlight 5 的IObservable< T> IObserver< T> 大量使用Rx和这些接口不是在Silverlight中定义的

In my actual project, I do not want to target Silverlight 5 as I use IObservable<T> and IObserver<T> heavily using Rx and these interfaces are not defined in Silverlight.

推荐答案

在添加/删除的Silverlight你所看到的,是两个不同的.NET API表面区域之间移植的翻转。我在这里介绍两种不同的表面面积:什么是.NET可移植子集(旧版)?

What you are seeing when you add/remove Silverlight, is portable flipping between two different .NET API surface areas. I cover these two different surface areas here: What is .NET Portable Subset (Legacy)?.

在我们所谓的遗产面积,此法住在委托。
。在新的表面面积,这种方法已被转移到一个MethodInfo。

In what we call the legacy surface area, this method lives on Delegate. In the new surface area, this method has been moved to MethodInfo.

我们为什么要这样做呢?

有关分层的原因。在新的表面,反射类型(例如大会的MemberInfo,MethodInfo的,等等)被视为生活在比芯基元的更高层,其包括代表。不同于传统表面积(他们都住在mscorlib程序),这几种不同的组件; System.Reflection.dll和System.Runtime.dll,分别为。

For layering reasons. In the new surface, reflection types (ie Assembly, MemberInfo, MethodInfo, etc) are considered as living in a higher layer than the core primitives, which includes Delegate. Unlike the legacy surface area (where they all live in mscorlib), these types in different assemblies; System.Reflection.dll and System.Runtime.dll, respectively.

这方法(几个人)在较低层造成一些(System.Runtime.dll)取决于一些在较高层(System.Reflection.dll)。为了防止这种情况,依赖了逆转。

This method (a few others) were causing something at a lower layer (System.Runtime.dll) to depend on something at a higher layer (System.Reflection.dll). To prevent that, the dependency was reversed.

这篇关于为什么我不能叫Delegate.CreateDelegate在我的便携式类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆