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:
- use t4. can use generate c# code based on input @ compile time.
- 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 usesgetproxyfor()
). - think underlying problem behind question (which didn't tell us) , solve in way.
Comments
Post a Comment