Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 }; 
like image 440
will Avatar asked Aug 01 '09 20:08

will


People also ask

Can an anonymous class implement an interface?

A normal class can implement any number of interfaces but the anonymous inner class can implement only one interface at a time.

When can you use anonymous classes?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Can anonymous class have constructor?

Since anonymous inner class has no name, an anonymous inner class cannot have an explicit constructor in Java.

What's meant by anonymous class?

A nested class that doesn't have any name is known as an anonymous class. Anonymous classes usually extend subclasses or implement interfaces. The above code creates an object, object1 , of an anonymous class at runtime. Note: Anonymous classes are defined inside an expression.


2 Answers

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. Nothing else. VB.Net allows slightly more customization in that the pairs can be mutable or immutable. 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.

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]. 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

like image 111
JaredPar Avatar answered Oct 11 '22 20:10

JaredPar


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.

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.

like image 21
Lasse V. Karlsen Avatar answered Oct 11 '22 22:10

Lasse V. Karlsen