Re: delegate instance

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"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"));


.



Relevant Pages

  • Re: delegate instance
    ... How can I use a delegate that I can set to call a non static function ... readDelegateReadFunction; ... public void read{ ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: delegate instance
    ... I dont think what I want to do is possible using non static functions. ... How can I use a delegate that I can set to call a non static function ... //use the ReadFunction delegate to acces the relevant function of reader ... ive expanded BinaryReader to read countless types. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delegates...?
    ... I skipped trying to implement a list of listeners because I wanted to emphasize the "delegate" concept. ... Note how I use the interface called "Delegate" to make a method than can be called by the Presenter. ... public void doIt() { ... private Delegate model; ...
    (comp.lang.java.programmer)
  • Re: Global cache in Win Apps
    ... > I have to make a global cache in a windows application to store few ... will run the delegate. ... public void SetData ...
    (microsoft.public.dotnet.framework)
  • Re: Generic Delegate Meltdown From Constraint
    ... I think that using a delegate is just making the code more difficult to implement, and it will likely make the code harder to understand later. ... public MyOwned(UnsubscribeDelegate unsubscribe) ... public void MakeOwned() ... // that an actual type is supplied for the generic type ...
    (microsoft.public.dotnet.languages.csharp)