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.

6 Answers 6

4

Here is another way, seems more direct.

object anon = Activator.CreateInstance(existingObject.GetType());
Sign up to request clarification or add additional context in comments.

2 Comments

What if you want to create a type that is not the same as the existing object?
@AlexHopeO'Connor: CreateInstance takes a Type object, so you can pass it any Type. For instance Activator.CreateInstance(typeof(string)).
3

Yes, there is. From memory:

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

object anon = create(existingAnonymousType);

2 Comments

Thanks, this looks like the only simple way.. Not a very simple thing to do though.
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.
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.

2 Comments

I think he means: when the type is not declared
Thanks, this sounds feasible... However, will that work for a silverlight app? Not sure if CodeDOM stuff is available there.
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.

1 Comment

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

1 Comment

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

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.