10

Is there any way to create C# 3.0 anonymous object via Reflection at runtime in .NET 3.5? I'd like to support them in my serialization scheme, so I need a way to manipulate them programmatically.

edited later to clarify the use case

An extra constraint is that I will be running all of it inside a Silverlight app, so extra runtimes are not an option, and not sure how generating code on the fly will work.

Michael Pliskin
  • 2,352
  • 4
  • 26
  • 42

6 Answers6

4

Here is another way, seems more direct.

object anon = Activator.CreateInstance(existingObject.GetType());
Guvante
  • 18,775
  • 1
  • 33
  • 64
3

Yes, there is. From memory:

public static T create<T>(T t)
{
    return Activator.CreateInstance<T>();
}

object anon = create(existingAnonymousType);
TraumaPony
  • 10,742
  • 12
  • 54
  • 74
  • Thanks, this looks like the only simple way.. Not a very simple thing to do though. – Michael Pliskin Sep 22 '08 at 12:57
  • Looking back at this, this will simply tie into existingAnonymousType's compile time type, not its run-time type, as the syntactic sugar of omitting the generic parameter is a compile time artifact. Put another way if it is defined as object you will create an instance of object. – Guvante Oct 08 '08 at 11:03
1

Use reflection to get the Type, use GetConstructor on the type, use Invoke on the constructor.

Edit: Thanks to Sklivvz for pointing out that I answered a question that wasn't asked ;)

The answer to the actual question: I've found that generating C# code and then using CodeDomProvider (but not CodeDOM itself -- terrible) and then compiling that down and reflecting types out of that is the easiest way of doing 'anonymous' objects at runtime.

Serafina Brocious
  • 30,433
  • 12
  • 89
  • 114
1

You might want to look into the DLR. I havn't done so myself (yet) but the use-case for the DLR (dynamic languages) sounds a lot like what you're trying to do.

Depending on what you want to do the Castle-framework's dynamic proxy object might be a good fit too.

Mendelt
  • 36,795
  • 6
  • 74
  • 97
1

You can use Reflection.Emit to generate the required classes dynamically, although it's pretty nasty to code up.

If you decide upon this route, I would suggest downloading the Reflection Emit Language Addin for .NET Reflector, as this allows you to see how existing classes would be built using Reflection.Emit, hence a good method for learning this corner of the framework.

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
1

You might also want to have a look into the FormatterServices class: MSDN entry on FormatterServices

It contains GetSafeUninitializedObject that will create an empty instance of the class, and several other handy methods when doing serialization.

In reply to comment from Michael: If you don't have the Type instance for type T, you can always get it from typeof(T). If you have an object of an unknown type, you can invoke GetType() on it in order to get the Type instance.

Kols
  • 3,641
  • 2
  • 34
  • 42
SteinNorheim
  • 2,197
  • 1
  • 15
  • 21