如何使用 ReSharper SDK 创建 [CustomAttribute(typeof(GenericType<,>))]?
有没有办法使用泛型类型的 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑问题在于您定义类声明的方式。下面是一个代码片段,它在上下文中使用
[ContractClass(typeof(Dictionary<,>))]
来装饰该类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<,>))]