T
tesvit
Hi there,
I am working on a class library that should incpect a given class,
make a delegate to a get Property and use it later on.
Here is the code snippet:
public delegate int MyClassDelegate(MyClass cls);
....
public MyClassDelegate GetGetDelegate(MyClass cls, string memberName)
{
Type objType = cls.GetType();
PropertyInfo propInfo = objType.GetProperty(memberName);
if (propInfo != null)
{
MethodInfo methodInfo = propInfo.GetGetMethod();
if (methodInfo != null)
{
/*1*/ Type dlgType = typeof(MyClassDelegate);
/*2*/ return (MyClassDelegate)Delegate.CreateDelegate(dlgType,
null, methodInfo);
}
else
return null;
}
else
return null;
}
....
Then lather on:
ClassName cls = ...;
string memberName = ...;
ClassDelegate dlg = GetGetDelegate(cls, memberName);
foreach( ClassName c in list)
{
int x = dlg(c);
...
}
Everything works fine.
However, I would like to avoid explicit usage of MyClass and
MyClassDelegate
There is enough information in MethodInfo above to create a delegate.
So, GetGetDelegate() should be:
public Delegate GetGetDelegate(string className, string memberName)
{
Type objType = Type.GetType(className);
...
/*1*/ Type dlgType = CreateDelegateType(methodInfo);
/*2*/ return Delegate.CreateDelegate(dlgType, null, methodInfo);
...
}
The CreateDelegateType() returns the appropriate type of the
MethodInfo.
That works fine too.
But how can I use a delegate now?
Delegate dlg = GetGetDelegate(cls, memberName);
// HOW TO CALL dlg ?
Note that I use this approach mainly because of performance, as
delegate direct call is much faster than dynamic invocation.
Thank you.
I am working on a class library that should incpect a given class,
make a delegate to a get Property and use it later on.
Here is the code snippet:
public delegate int MyClassDelegate(MyClass cls);
....
public MyClassDelegate GetGetDelegate(MyClass cls, string memberName)
{
Type objType = cls.GetType();
PropertyInfo propInfo = objType.GetProperty(memberName);
if (propInfo != null)
{
MethodInfo methodInfo = propInfo.GetGetMethod();
if (methodInfo != null)
{
/*1*/ Type dlgType = typeof(MyClassDelegate);
/*2*/ return (MyClassDelegate)Delegate.CreateDelegate(dlgType,
null, methodInfo);
}
else
return null;
}
else
return null;
}
....
Then lather on:
ClassName cls = ...;
string memberName = ...;
ClassDelegate dlg = GetGetDelegate(cls, memberName);
foreach( ClassName c in list)
{
int x = dlg(c);
...
}
Everything works fine.
However, I would like to avoid explicit usage of MyClass and
MyClassDelegate
There is enough information in MethodInfo above to create a delegate.
So, GetGetDelegate() should be:
public Delegate GetGetDelegate(string className, string memberName)
{
Type objType = Type.GetType(className);
...
/*1*/ Type dlgType = CreateDelegateType(methodInfo);
/*2*/ return Delegate.CreateDelegate(dlgType, null, methodInfo);
...
}
The CreateDelegateType() returns the appropriate type of the
MethodInfo.
That works fine too.
But how can I use a delegate now?
Delegate dlg = GetGetDelegate(cls, memberName);
// HOW TO CALL dlg ?
Note that I use this approach mainly because of performance, as
delegate direct call is much faster than dynamic invocation.
Thank you.