delegate instance

  • Thread starter Thread starter colin
  • Start date Start date
C

colin

Hi,

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*/);

this is ofc simplified,
ive expanded BinaryReader to read countless types.

thanks
Colin =^.^=
 
colin said:
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"));
 
Hi,

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*/);

this is ofc simplified,
ive expanded BinaryReader to read countless types.

thanks
Colin =^.^=

Hi,

public delegate T readDelegate<T>();
public class TypeTeader<T> {
readDelegate<T> ReadFunction;
public readDelegate<T> SetDelegate{
get{return ReadFunction;}
set { ReadFunction = value; }
}
public void read(T p) {
if (ReadFunction != null)
ReadFunction();
}
}
 
thanks,
I was trying to avoid using reflection,
Ive used reflection extensivly and its proved far too slow,
Ive directly coded for the most common structures and its a lot faster.

but now I wish to make it easier to add more directly coded conversions.

however this does give me an idea I could cache the reflection info rather
than
fetch it every time I read in a variable.

im considering either making a class containing static functions instead of
members,
or to have a seperate delegate for each instance ie one set for each file,
this sounds a bit over complicated.

I might try this aproach to see how fast it is though thanks :)

Idealy I would like to use the c++ type of member pointer,
even though I always seemed to have a real trouble trying to remember the
syntax,
I just assumed it was difuclt in c#, but ive been looking into various
things behind the language
and see why its not doable.

Colin =^.^=
 
Hi,
I cant work out how your code can speciify the relevant instance
of BinaryReader when calling the delegate.

I dont think what I want to do is possible using non static functions.
I gues il have to change everything to static functions.
however this also poses problems as static functions cant be overriden.

Ive also run into problems with using MethodInfo becuase I need to pass an
argument as a ref,
although it runs ok the ref is boxed wich no longer is a ref to the original
object >.<

thanks
Colin =^.^=


message
Hi,

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*/);

this is ofc simplified,
ive expanded BinaryReader to read countless types.

thanks
Colin =^.^=

Hi,

public delegate T readDelegate<T>();
public class TypeTeader<T> {
readDelegate<T> ReadFunction;
public readDelegate<T> SetDelegate{
get{return ReadFunction;}
set { ReadFunction = value; }
}
public void read(T p) {
if (ReadFunction != null)
ReadFunction();
}
}
 
Ive found a way to do it neatly, fast and type safe.
it also ensures the defualt type ties up with the read type.

public class UStruct
{
public delegate ValueType DefualtReadDelegate(SerializeStream file);
public static Dictionary<string, DefualtReadDelegate> NativeStructs =
GetNativeStructs();
public DefualtReadDelegate defualtReadDelegate;
private static Dictionary<string, DefualtReadDelegate> GetNativeStructs()
{
NativeStructs = new Dictionary<string, DefualtReadDelegate>();
NativeStructs.Add("PointRegion", delegate(SerializeStream file)
{ PointRegion x = default(PointRegion); if (file != null)
file.Serialize(ref x); return x; });
NativeStructs.Add("Scale", delegate(SerializeStream file)
{ UScale x = default(UScale); if (file != null) file.Serialize(ref x);
return x; });
NativeStructs.Add("Vector", delegate(SerializeStream file)
{ Vector x = default(Vector); if (file != null) file.Serialize(ref x);
return x; });
NativeStructs.Add("Color", delegate(SerializeStream file)
{ FColor x = default(FColor); if (file != null) file.Serialize(ref x);
return x; });
NativeStructs.Add("Rotator", delegate(SerializeStream file)
{ Rotator x = default(Rotator); if (file != null) file.Serialize(ref x);
return x; });
NativeStructs.Add("ADrop", delegate(SerializeStream file)
{ ADrop x = default(ADrop); if (file != null) file.Serialize(ref x);
return x; });
return NativeStructs;
}
public UStruct(string s)
{
NativeStructs.TryGetValue(Name, out defualtReadDelegate);
}
public ValueType Defualt(string StructName)
{
if (defualtReadDelegate != null)
return defualtReadDelegate(null);
...get custom build defualt...
}
}

public class Property
{
private void ReadStruct(SerializeReader file,UObject uObj,UData udata)
{//would be easier if there was a direct reference to UStruct
UStruct.DefualtReadDelegate found;
if (UStruct.NativeStructs.TryGetValue(StructName, out found))
{
udata.data = found(file);
return;
}
... read custom
}
}

il need to add write capability too, the SerializeStream is the base class
for read and write,
and can handle both ok.
im not sure how the format of the data to write will need to be yet.

thanks for the helps
Colin =^.^=
 
colin said:
thanks,
I was trying to avoid using reflection,
Ive used reflection extensivly and its proved far too slow,
Ive directly coded for the most common structures and its a lot
faster.
but now I wish to make it easier to add more directly coded
conversions.
however this does give me an idea I could cache the reflection info
rather than
fetch it every time I read in a variable.

im considering either making a class containing static functions
instead of members,
or to have a seperate delegate for each instance ie one set for each
file, this sounds a bit over complicated.

I might try this aproach to see how fast it is though thanks :)

Idealy I would like to use the c++ type of member pointer,
even though I always seemed to have a real trouble trying to remember
the syntax,
I just assumed it was difuclt in c#, but ive been looking into various
things behind the language
and see why its not doable.

It is doable, it is called an "open delegate". You have to use reflection
to create it, but then you have a real delegate that runs at full speed.

See http://msdn2.microsoft.com/en-us/library/53cz7sc6.aspx
 
oooh that looks cool, I didnt come accros that,
theres quite a lot to read...
why is the information I need always so hard to find that I have to
make so many posts here lol.

many thanks
Colin =^.^=
 
Back
Top