1

Given the interface below:

public interface ITest
{
    string PropTest1 { get; set; }
    int PropTest2 { get; set; }
}

How can I create a dynamic proxy class that will implement the interface ITest and return values for both properties?

Also, would this be slow for a production system?

Thanks

2 Answers 2

2

How can I create a dynamic proxy class that will implement the interface ITest and return values for both properties?

Have a look at mocking librariyes, i.e Rhino mocks or Moq - this is exactly what these allow you to do (and then some) - no need to re-invent the wheel.

Since this is using reflection to create the proxy it will be very slow compared to regular code - it all depends on the scenario whether this is acceptable or not.

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

Comments

0

as for perf, generating the type might take a little bit of time but once you have it, making instances of it should not be much slower than making instances of any other class.

if you do choose do make your own dynamic proxy, you will need to learn IL. reflector or a similar tool is a great way of doing that. you can write the code you'd like to generate, and then look at the IL in reflector

as @BrokenGlass says though, there are libraries out there to do this, but its a great learning experience :)

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.