2011-05-27 66 views
1

任何人都知道为什么这不会编译?问题与从工作流程活动继承有关。错误编译与泛型和工作流活动基类

public class MyActivityBase<T> : System.Workflow.ComponentModel.Activity 
{ 
    public T MyProperty { get; set; } 
} 

编译错误消息

 
Error 1 Could not create activity of type '...Activities.Common.MyActivityBase`1'. System.ArgumentException: Cannot create an instance of ...Activities.Common.MyActivityBase`1[T] because Type.**ContainsGenericParameters is true**. 
    at System.RuntimeType.CreateInstanceCheckThis() 
    at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) 
    at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) 
    at System.Activator.CreateInstance(Type type, Boolean nonPublic) 
    at System.Workflow.ComponentModel.Compiler.XomlCompilerHelper.InternalCompileFromDomBatch(String[] files, String[] codeFiles, WorkflowCompilerParameters parameters, WorkflowCompilerResults results, String localAssemblyPath) 

谢谢, Ť

+0

Hello Thomas。我想提请你注意一些事情。一旦提供可接受的答案,接受答案是很常见的做法。您可以回到以前的问题并单击所需答案左侧的大型支票图片来接受答案。你接受的答案越多,人们就越有可能想回答你未来的问题。查看FAQ获取更多帮助。 – MPelletier 2011-05-27 20:37:28

回答

3

你的类型被通过反射实例化。无论代码在做什么,实例化都不知道如何使用通用参数实例化一个类型。因此它会抛出一个异常。

不幸的是,如果您想直接使用MyActivityBase<T>,那么您确实无能为力。您可能可以通过使用基类的具体子实现来获得(类似于public class MyActivityString : MyActivityBase<string>