我正在 C# 中创建可组合的 WinRT 组件,并尝试添加虚拟方法定义:
namespace FooComponent {
[EnableComposition]
public class Foo {
public virtual void Bar() { }
}
}
我收到 winmd 导出实用程序的错误,指出“托管 WinRT 组件中不允许使用虚拟方法声明”。但在 C++ 中创建这样的组件确实很容易。
限制在托管代码中定义的可组合 WinRT 组件中定义自定义虚拟方法的能力的原因是什么?在没有自定义虚拟方法的情况下启用实现继承的能力的目的是什么?
I am creating composable WinRT component in C# and trying to add virtual method definition:
namespace FooComponent {
[EnableComposition]
public class Foo {
public virtual void Bar() { }
}
}
I've got an error from winmd export utility standing that "virtual method declaration is not allowed in managed WinRT components". But it is really easy to create such component in C++.
What is the reason to restict the ability to define custom virtual methods in a composable WinRT components, defined in managed code? What is the purpose of ability to enable implementation inheritance without custom virtual methods?
发布评论
评论(1)
如果您尝试构建将由 JavaScript 使用的 Windows 运行时组件,那么您将无法执行您尝试执行的操作。这样做时需要遵循一组规则。也就是说,您的类必须标记为“密封”,这将阻止您拥有任何虚拟方法。请参阅 MSDN 上有关为 JS 创建 Windows 运行时组件的文档 - http://msdn.microsoft.com/en-us/library/windows/apps/br230301(v=vs.110).aspx
如果您不尝试创建运行时组件对于 JS,那么您可以将库的输出类型更改为“类库”,以允许您执行您正在尝试的操作。
If you are attempting to build a Windows Runtime component that will be consumed by JavaScript, then you won't be able to do what you're attempting to. There are a set of rules to follow when doing so. Namely which is that your class must be marked as "sealed" which would prevent you from having any virtual methods anyways. Please see the documentation on MSDN that talks about creating Windows Runtime Components for JS - http://msdn.microsoft.com/en-us/library/windows/apps/br230301(v=vs.110).aspx
If you are not attempting to create a runtime component for JS, then you can change the output type of your library to "Class Library" to allow you to do what you are attempting.