1

Recently I have been looking for a way to create proxy objects that reflect the properties of other objects programmatically. However I have been unable to find any information on how to do something like this, I believe it should be possible as I have noticed in EntityFramework performing LINQ operations returns proxy objects with the same properties which can be cast directly to the type that they represent.

Basically I am looking for this same functionality and if anyone could explain how it works I would be greatful.

Pseudo code:

object proxy = GetDynamicProxy(typeof(ProxyType));

So basically I am looking for a way to get an object that represents the ProxyType and a method like GetDynamicProxy to get it.

Thanks, Alex.

7
  • Can you provide some code or pseudocode to clarify what you are trying to do? Commented Jun 6, 2012 at 2:36
  • Do you mean ICloneable/MemberwiseClone? Commented Jun 6, 2012 at 2:40
  • Added some pseudo code to try and make my desires clearer. Commented Jun 6, 2012 at 2:41
  • How's the pseudo different to Activator.CreateInstance? Commented Jun 6, 2012 at 2:49
  • Well the object created will not be the same Type as the ProxyType, it will be a POCO object, which just reflects the properties, removing all events and interfaces. Commented Jun 6, 2012 at 3:21

2 Answers 2

2

In EF the proxy are generated by dynamically deriving from the entity type and dynamically implementing the code. You need to be looking at dynamically compiled assemblies, types and lambdas. System.Reflection.Emit is your friend (???). Here is a link that should help http://msdn.microsoft.com/en-us/library/3y322t50

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

Comments

0

If your objectcontext has ProxyCreationEnabled set to true then

yourContext.CreateObject<YourType>()

will create a dynamic proxy version of your type and you can populate the data.

http://msdn.microsoft.com/en-us/library/dd456853.aspx

Comments

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.