I use a method similar to the following to get some precomposed metadata related to one type of property.
MyData GetProperty & lt; T, U & gt; (Expression & lt; Func & lt; T, U & gt; Member) {// Get property referenced in Lambda expression; Member expression expression = Member member; PropertyInfo property = expression. // Get properties in propertiesT property info [] candidates = typef (t). GetProperties (BindingFlag.public | BindingFlag.Instance); // Match Tested (Property Info Candidate in Candidates) Get (Candidate == Property) GetMetaData & lt; T & gt; (Candidate); New exception throw ("property not found."); } // Recharge precomposed metadata MyData GetMetaData & lt; T & gt; (PropertyInfo Properties) {...} As you expect, it works when used as:
Miscellaneous data = PropertyEfO ((Employee E) = & gt; E. Name); but not used in the following general method:
zero MyGenericMethod & lt; T & gt; (Int id) where T: IEmployee {var data = PropertyInfo ((tE) => E. nom); } This fails because the declared type of property in the first method is now IEmployee , therefore there is no property in Lambda ' How can I match them without relying on the name of the property that does not match the property in the type? (If the interface is clearly implemented, then there may be several properties with the same name, so p1.Name == p2.Name will not cut it).
What you wanted is a . You can get the code from GetInterfaceMap (typef (interface)), ie,
interface mapping = typef (employee) .GetInterfaceMap (typeof (IEmployee); Now, the mapping will include the field interface management , which will be the way you see the interface, and TargetMethods which are Methods of implementation of the class Note that this map is for recipients of the recipients of the recipients methods from the target class. You must find the appropriate interface properties to map the recipient method to the different properties of the class of recipient method.
type interface type = type (IIMOP); Class type type = employee (employee); PropertyInfo NameProperty = Interface Type GetProperty ("name"); MethodInfo nameGetter = nameProperty.GetGetMethod (); Interfacemapping mapping = classtype GETInterfaceMap (InterfaceTip); MethodInfo targetMethod = null; For (Int i = 0; I & lt; Mapping.InterfaceMason.Langith; I ++) {if (Mapping Interface Matthews [I] ==Namegator) {targetMethod = Mapping Levelmaption [ii]; break; }} PropertyInfo targetProperty = null; PropertyInfo Property in Foreach (ClassIntex.GetProperties (BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic)) // Includes non-public! {If (targetMethod == property.GetGetMethod (true)) // included non-public! {TargetProperty = Property; break; }} // targetProperty is the real property Caution: BindingFlags.NonPublic and GetGetMethod (true) Here, if you have got a clear interface implementation to reach private members, then there really is no public asset matching the property of the interface, instead of Some.NameSpace.IEmployee.Name Is a private property that is mapped (which is, of course, your explicit implementation).
When you have got the right property, you can just call
parameter expiration p = expression.pameter ("e" typeof (t)); Expression & lt; Funk & lt; T, U & gt; & Gt; Lambda = expression. Lambda & lt; Funk & lt; T, U & gt; & Gt; (Expression Property (P, Target Property), P); And you've got yourself a lambda expression that uses the properties of the class instead of the properties of the interface.
Comments
Post a Comment