views:

4241

answers:

6

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.

+1  A: 

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.

Cody Brocious
I think he means: when the type is not declared
Sklivvz
Thanks, this sounds feasible... However, will that work for a silverlight app? Not sure if CodeDOM stuff is available there.
Michael Pliskin
+1  A: 

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
Thanks, sounds interesting. However, probably a no-go for me as I am planning to use it in a Silverlight app.
Michael Pliskin
+3  A: 

Yes, there is. From memory:

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

object anon = create(existingAnonymousType);
TraumaPony
Thanks, this looks like the only simple way.. Not a very simple thing to do though.
Michael Pliskin
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
+1  A: 

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
Thanks, that's doable, but it probably won't work together with existing anonymous classes I believe.
Michael Pliskin
+1  A: 

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.

norheim.se
The problem with it is that I don't have a Type instance. But thanks for the tip, worths looking at anyway.
Michael Pliskin
Yes, the GetType thing makes sense. So looks like I just need to create an example of each object and copy them later.
Michael Pliskin
+3  A: 

Here is another way, seems more direct.

object anon = Activator.CreateInstance(existingObject.GetType());
Guvante
.....Yeah, or that.
TraumaPony