Friday, 23 August 2013

Infer interface by type at compile time

Infer interface by type at compile time

Is there any way to infer an interface from an object based on its type.
For instance if I had the following object:
public class Person
{
public string FirstName
{ get; set; }
public string LastName
{ get; set; }
}
I would like to be able to infer this interface:
public interface IPerson
{
string FirstName
{ get; set; }
string LastName
{ get; set; }
}
What I would like to do is be able to create a generic proxy factory,
using reflection.emit, that adheres to the inferred interface at compile
time. I know that I could instead return a dynamic object with all the
object's properties, but that is all handled at runtime vs. compilation.

No comments:

Post a Comment