简体   繁体   English

我可以将属性与匿名类一起使用吗?

[英]Can I use Attributes with Anonymous classes?

I have a anonymous class: 我有一个匿名班:

var someAnonymousClass = new
{
    SomeInt = 25,
    SomeString = "Hello anonymous Classes!",
    SomeDate = DateTime.Now
};

Is there anyway to attach Attributes to this class? 无论如何,将属性附加到此类? Reflection, other? 反思,其他? I was really hoping for something like this: 我真的很希望这样的事情:

var someAnonymousClass = new
{
    [MyAttribute()]
    SomeInt = 25,
    SomeString = "Hello anonymous Classes!",
    SomeDate = DateTime.Now
};

You're actually creating what is called an anonymous type here, not a dynamic one. 您实际上是在这里创建所谓的匿名类型,而不是动态类型。

Unfortunately no there is no way to achieve what you are trying to do. 不幸的是,没有任何方法可以实现您想做的事情。 Anonymous types are meant to be a very simple immutable type consisting of name / value pairs. 匿名类型是由名称/值对组成的非常简单的不可变类型。

The C# version of anonymous type only allows you to customize the set of name / value pairs on the underlying type. C#版本的匿名类型仅允许您自定义基础类型上的一组名称/值对。 Nothing else. 没有其他的。 VB.Net allows slightly more customization in that the pairs can be mutable or immutable. VB.Net允许更多的自定义,因为这些对可以是可变的或不变的。 Neither allow you to augment the type with attributes though. 两者都不允许您使用属性来扩充类型。

If you want to add attributes you'll need to create a full type. 如果要添加属性,则需要创建一个完整类型。

EDIT OP asked if the attributes could be added via reflection. 编辑 OP询问是否可以通过反射添加属性。

No this cannot be done. 不,这无法完成。 Reflection is a way of inspecting metadata not mutating it. 反射是一种检查元数据而不对其进行变异的方法。 Hence it cannot be used to add attributes. 因此,它不能用于添加属性。

Additionally, type definitions in an assembly, and in general, are immutable and cannot be mutated at runtime [1]. 另外,程序集中的类型定义通常是不可变的,并且在运行时无法更改[1]。 This includes the adding of attributes to a method. 这包括将属性添加到方法。 So other reflection like technologies cannot be used here either. 因此,此处也无法使用其他类似反射的技术。

[1] The one exception to this is ENC operation [1]唯一的例外是ENC操作

First of all, this is an anonymous type. 首先,这是一个匿名类型。 The word "dynamic" might lead people to think you're talking about a C# 4.0 class implementing dynamic semantics, which you aren't. “动态”一词可能使人们认为您在谈论的是实现动态语义的C#4.0类,而实际上并不是。

Secondly, no, you're not able to do what you ask. 其次,不,你不能做你所要求的。

If you need to specify attributes for your properties, you're back to a named type, ie. 如果需要为属性指定属性,则返回到命名类型,即。 a normal class or struct. 普通的类或结构。

It is possible to add attributes to an anonymous instance using TypeDescriptor.AddAttributes . 可以使用TypeDescriptor.AddAttributes将属性添加到匿名实例。 You can then later access the attributes using TypeDescriptor.GetAttributes . 然后,您可以稍后使用TypeDescriptor.GetAttributes访问属性。

This will not add them to the Type instance for the object. 这不会将它们添加到对象的Type实例中。 So it may not be useful in your case if you do not control the code that retrieves and applies the attributes. 因此,如果您不控制检索和应用属性的代码,则对您而言可能没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM