1

So I have a anonymous type which I have used ToString on, and put into a file. It looks like this (and remarkably like JSon...):

{ Name = Name, Description = Description, Status = Pending, Key = Template, SubmittedBy = MyName, Identity = 2fb7a40b-e07a-4f1a-a48b-2c2720567f35 }

now I want to go the other way (edit:take the string from a file and put it into a format I can put into a known object) and put it into an anonymous type (which has the same properties, but I don't care if they are castable or anything like that, I just want a quick way to do something like.

AnonObject = GetObjectFromFile(blah);
RealObject.Name = AnonObject.Name;

I only have an interface for RealObject, so I can't add serialisation to the class. If you know a better way, let me know. Thanks.

3
  • Is Identity a guid? Looks like it. Commented Jun 27, 2012 at 15:08
  • 2
    You may be able to use a JSON parser such as JSON.net. Commented Jun 27, 2012 at 15:09
  • Identity is a guid. I don't want to add a library if I can help it. Commented Jun 27, 2012 at 15:13

1 Answer 1

6

Anonymous types are defined at compile time, and included in the assembly just like any other type. Creating them at execution time would require dynamically creating types - it's likely to end up getting messy really quickly.

If you're using .NET 4, you could use an ExpandoObject and dynamic; otherwise I'd probably just use a Dictionary<string, object> and change this:

RealObject.Name = AnonObject.Name;

to

RealObject["Name"] = AnonObject.Name;

(Or vice versa. It's not really clear what you're doing.)

Sign up to request clarification or add additional context in comments.

2 Comments

Does ExpandoObject serialise?
Okay, it doesn't. I'll just manually parse the string into RealObject rather than using an intermediary. Thanks.

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.