C# 如何使用ReSharper SDK创建[CustomAttribute(typeof(GenericType<;,>;))]?

C# 如何使用ReSharper SDK创建[CustomAttribute(typeof(GenericType<;,>;))]?,c#,.net,resharper,resharper-6.1,resharper-sdk,C#,.net,Resharper,Resharper 6.1,Resharper Sdk,有没有办法用泛型类型的typeof表达式创建属性 以下代码仅部分起作用: CSharpElementFactory工厂=。。。 IClassDeclaration类型声明=。。。 IClassDeclaration类声明=。。。 IType[]属性参数= (来自classDeclaration.TypeParameters中的typeParameter) 选择(IType)TypeFactory.CreateUnknownType(模块)).ToArray(); IType classType=

有没有办法用泛型类型的
typeof
表达式创建属性

以下代码仅部分起作用:

CSharpElementFactory工厂=。。。
IClassDeclaration类型声明=。。。
IClassDeclaration类声明=。。。
IType[]属性参数=
(来自classDeclaration.TypeParameters中的typeParameter)
选择(IType)TypeFactory.CreateUnknownType(模块)).ToArray();
IType classType=TypeFactory.CreateType(classDeclaration.DeclaredElement,
属性(参数);
var属性=factory.CreateAttribute(
新的特殊属性(
ClrTypeNames.ContractClassAttribute,
模块,
()=>new[]{newattributevalue(classType)},
可枚举。空);
typeDeclaration.AddAttributeAfter(属性,null);

我怀疑问题在于您定义类声明的方式。下面是一个代码片段,它在上下文中用
[ContractClass(typeof(Dictionary))]

ClrTypeName合同类属性=
新的ClrTypeName(“System.Diagnostics.Contracts.ContractClassAttribute”);
ClrTypeName someGenericClass=新的ClrTypeName(“System.Collections.Generic.Dictionary`2”);
var module=provider.PsiModule;
var owner=provider.GetSelectedElement(true,true);
var factory=csharplementfactory.GetInstance(模块);
var someGenericTypeElement=TypeElementUtil.GetTypeElementByClrName(someGenericClass,模块);
var unknownType=TypeFactory.CreateUnknownType(模块);
var someGenericType=TypeFactory.CreateType(someGenericTypeElement,unknownType,unknownType);
var contractClassTypeElement=TypeElementUtil.GetTypeElementByClrName(contractClassAttribute,模块);
var attribute=factory.CreateAttribute(contractClassTypeElement,new[]{new AttributeValue(someGenericType)},
EmptyArray.Instance);
owner.AddAttributeAfter(属性,null);

TypeElementUtil.GetTypeElementByClrName(someGenericClass,module)
为空。因此
TypeFactory.CreateType(someGenericTypeElement…
引发异常。您使用的是什么版本的.Net?(我的意思是项目的.Net framework目标。)您的代码段可以工作,因为您使用的是现有类型,但在我的例子中,我创建的是一个全新的泛型类,应该由
typeof(GenericType)引用
在一个属性中。好的,那么假设您有一个现有的类型。在本例中,您只需使用它的声明:
var someGenericType=TypeFactory.CreateType(someDeclarationhave.DeclaredElement,…)
ClrTypeName contractClassAttribute = 
  new ClrTypeName("System.Diagnostics.Contracts.ContractClassAttribute");
ClrTypeName someGenericClass = new ClrTypeName("System.Collections.Generic.Dictionary`2");

var module = provider.PsiModule;
var owner = provider.GetSelectedElement<IClassDeclaration>(true, true);
var factory = CSharpElementFactory.GetInstance(module);

var someGenericTypeElement = TypeElementUtil.GetTypeElementByClrName(someGenericClass, module);
var unknownType = TypeFactory.CreateUnknownType(module);
var someGenericType = TypeFactory.CreateType(someGenericTypeElement, unknownType, unknownType);
var contractClassTypeElement = TypeElementUtil.GetTypeElementByClrName(contractClassAttribute, module);
var attribute = factory.CreateAttribute(contractClassTypeElement, new[] {new AttributeValue(someGenericType)},
                                        EmptyArray<Pair<string, AttributeValue>>.Instance);
owner.AddAttributeAfter(attribute, null);