c# - Infer interface by type at compile time -


is there way infer interface object based on type. instance if had following object:

public class person {   public string firstname   { get; set; }    public string lastname   { get; set; } } 

i able infer interface:

public interface iperson {   string firstname   { get; set; }    string lastname   { get; set; } } 

what able create generic proxy factory, using reflection.emit, adheres inferred interface @ compile time. know instead return dynamic object object's properties, handled @ runtime vs. compilation.

edit

a little more explanation i'm trying achieve. want create proxy class has method signature this:

public t getproxyfor<u>(u someobject); 

that way user can call getproxyfor(people) , peopleproxy object implement properties of people object (or other object). way, using code call peopleproxy.lastname , compile, call peopleproxy.age result in error when compiled since doesn't exist.

i'm new reflection , emitting, i'm wanting may not possible.

basically, you're asking execute code creates interface @ compile time.

your options are:

  1. use t4. can use generate c# code based on input @ compile time.
  2. use f# type providers. that's want: can execute code @ compile time , generate interface right when write code calls getproxyfor(). there nothing in c#, require switch f# (at least part of code uses getproxyfor()).
  3. think underlying problem behind question (which didn't tell us) , solve in way.

Comments

Popular posts from this blog

Line ending issue with Mercurial or Visual Studio -

java - Jtable duplicate Rows -

java - Run a .jar on Heroku -