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

发布于 2024-12-27 05:37:10 字数 871 浏览 3 评论 0 原文

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

以下代码仅部分有效:

CSharpElementFactory factory = ...
IClassDeclaration typeDeclaration = ...

IClassDeclaration classDeclaration = ...
IType[] attributeTypeParameters = 
    (from typeParameter in classDeclaration.TypeParameters
    select (IType)TypeFactory.CreateUnknownType(module)).ToArray();
IType classType = TypeFactory.CreateType(classDeclaration.DeclaredElement, 
                                         attributeTypeParameters);

var attribute = factory.CreateAttribute(
    new SpecialAttributeInstance(
        ClrTypeNames.ContractClassAttribute,
        module,
        () => new[] { new AttributeValue(classType) },
        Enumerable.Empty<Pair<string, AttributeValue>>));
typeDeclaration.AddAttributeAfter(attribute, null);

Is there a way to create an attribute with a typeof expression with a generic type?

The following code works only partially:

CSharpElementFactory factory = ...
IClassDeclaration typeDeclaration = ...

IClassDeclaration classDeclaration = ...
IType[] attributeTypeParameters = 
    (from typeParameter in classDeclaration.TypeParameters
    select (IType)TypeFactory.CreateUnknownType(module)).ToArray();
IType classType = TypeFactory.CreateType(classDeclaration.DeclaredElement, 
                                         attributeTypeParameters);

var attribute = factory.CreateAttribute(
    new SpecialAttributeInstance(
        ClrTypeNames.ContractClassAttribute,
        module,
        () => new[] { new AttributeValue(classType) },
        Enumerable.Empty<Pair<string, AttributeValue>>));
typeDeclaration.AddAttributeAfter(attribute, null);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-01-03 05:37:10

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

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);

I suspect the problem is the way in which you are defining your class declaration. Here's a code snippet which decorates the class in context with [ContractClass(typeof(Dictionary<,>))]

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文