Re: delegate instance
- From: "Alberto Poblacion" <earthling-quitaestoparacontestar@xxxxxxxxxxxxx>
- Date: Wed, 23 Apr 2008 18:30:30 +0200
"colin" <colin.rowe1@xxxxxxxxxxxxxxxxxx> wrote in message news:l5JPj.57201$h65.47699@xxxxxxxxxxxxxxxxxxxxxxx
How can I use a delegate that I can set to call a non static function
but of any instance of the class ?
eg
class TypeTeader<T>
{
delegate T readDelegate<T>();
readDelegate<T> ReadFunction;
public TypeTeader(readDelegate d)
{
ReadFunction=d;
}
public void read<T>(BinaryReader reader,ref T p)
{
//use the ReadFunction delegate to acces the relevant function of reader
}
}
TypeReader<Int32> Int32reader(/*pass ReadInt32 function of BinaryReader*/);
If I have understood what you want, I think that you can achieve it by means of a MethodInfo instead of a delegate:
using System.Reflection;
....
class TypeTeader<T>
{
MethodInfo ReadFunction;
public TypeReader(MethodInfo d)
{
ReadFunction=d;
}
public void read<T>(BinaryReader reader,ref T p)
{
p = (T)ReadFunction.Invoke(reader, null);
}
}
....
Type t = typeof(BinaryReader);
TypeReader<Int32> Int32reader = new TypeReader<Int32>(t.GetMethod("ReadInt32"));
.
- Follow-Ups:
- Re: delegate instance
- From: colin
- Re: delegate instance
- References:
- delegate instance
- From: colin
- delegate instance
- Prev by Date: delegate instance
- Next by Date: Font not printing right on SOME printers
- Previous by thread: delegate instance
- Next by thread: Re: delegate instance
- Index(es):
Relevant Pages
|