InvokeMember不理解?誰能解釋一下 - .NET技術 - C#
.net - C# Invoke a method with [Type].InvokeMember() in a separate Thread - Stack Overflow
Listing 6 (C#): Dynamic access methods in the ComUtils class
public static object GetProperty(object loObject,
string lcProperty) {
return loObject.GetType().InvokeMember(lcProperty,
BindingFlags.GetProperty,null,loObject,null);
}
public static object SetProperty(object loObject,
string lcProperty,params object[] loValue) {
return loObject.GetType().InvokeMember(lcProperty,
BindingFlags.SetProperty,null,loObject,loValue);
}
public static object CallMethod(object loObject,
string lcMethod, params object[] loParams) {
return loObject.GetType().InvokeMember(lcMethod,
BindingFlags.InvokeMethod,null,loObject,loParams);
}
With this method we can now return the property value as:
public string ReturnCustomer(object loObject) {
//*** loObject is passed from VFP – retrieve Company field and return
return (string) ComUtils.GetProperty(loObject,"Company");
}
If you have to return multiple object levels (ie. loObject.oData.Company) you have to drill into each object individually. For example:
public string CallAndReturnCustomer(object loObject) {
bool llResult = (bool) ComUtils.CallMethod(loObject,"Load",4)
Object loData = ComUtils.GetProperty(loObject,"oData")
return (string) ComUtils.GetProperty(loData,"Company");
留言列表