471,325 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

delegate instance

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<TReadFunction;
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<Int32Int32reader(/*pass ReadInt32 function of BinaryReader*/);

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

thanks
Colin =^.^=
Jun 27 '08 #1
7 1286
"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:l5*******************@newsfe2-gui.ntli.net...
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<TReadFunction;
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<Int32Int32reader(/*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<Int32Int32reader = new
TypeReader<Int32>(t.GetMethod("ReadInt32"));
Jun 27 '08 #2
On Apr 23, 12:03*pm, "colin" <colin.ro...@ntworld.NOSPAM.comwrote:
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<TReadFunction;
*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<Int32Int32reader(/*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<TReadFunction;
public readDelegate<TSetDelegate{
get{return ReadFunction;}
set { ReadFunction = value; }
}
public void read(T p) {
if (ReadFunction != null)
ReadFunction();
}
}
Jun 27 '08 #3
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 =^.^=
"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:%2****************@TK2MSFTNGP03.phx.gbl...
"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:l5*******************@newsfe2-gui.ntli.net...
>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<TReadFunction;
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<Int32Int32reader(/*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<Int32Int32reader = new
TypeReader<Int32>(t.GetMethod("ReadInt32"));


Jun 27 '08 #4
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 =^.^=
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:83**********************************@26g2000h sk.googlegroups.com...
On Apr 23, 12:03 pm, "colin" <colin.ro...@ntworld.NOSPAM.comwrote:
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<TReadFunction;
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<Int32Int32reader(/*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<TReadFunction;
public readDelegate<TSetDelegate{
get{return ReadFunction;}
set { ReadFunction = value; }
}
public void read(T p) {
if (ReadFunction != null)
ReadFunction();
}
}
Jun 27 '08 #5
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, DefualtReadDelegateNativeStructs =
GetNativeStructs();
public DefualtReadDelegate defualtReadDelegate;
private static Dictionary<string, DefualtReadDelegateGetNativeStructs()
{
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 =^.^=
Jun 27 '08 #6
colin wrote:
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
>
Colin =^.^=
"Alberto Poblacion" <ea******************************@poblacion.org>
wrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
>"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:l5*******************@newsfe2-gui.ntli.net...
>>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<TReadFunction;
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<Int32Int32reader(/*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<Int32Int32reader = new
TypeReader<Int32>(t.GetMethod("ReadInt32"));

Jun 27 '08 #7
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 =^.^=
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
colin wrote:
>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
>>
Colin =^.^=
"Alberto Poblacion" <ea******************************@poblacion.org>
wrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
>>"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:l5*******************@newsfe2-gui.ntli.net...
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<TReadFunction;
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<Int32Int32reader(/*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<Int32Int32reader = new
TypeReader<Int32>(t.GetMethod("ReadInt32"));


Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Doug Handler | last post: by
11 posts views Thread by ohmmega | last post: by
3 posts views Thread by Bob Speaking | last post: by
26 posts views Thread by raylopez99 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.