473,246 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 1396
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel | last post by:
How do I call a delegate in late bound C# dll? there some way to do this w/ a sharedinterface file? any examples? i tried this but it doesnt work: (oType.GetMethod("IOCTLJOB").Invoke(pObj, new...
3
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object....
2
by: Nima | last post by:
The documentation for the Delegate and the MultiCastDelegate classes tell me that when we have a line like: public delegate void CheckAndPrintDelegate(string str); it causes the compiler to...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
5
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
11
by: ohmmega | last post by:
hello world. i would like to implement a class with a timer, witch informs me every second about it's tick. the code works already, but i would like to change a thing (or more). <code> //at...
3
by: Bob Speaking | last post by:
Hi at all, Is possible to pass a parameter though a delegate or to override it? (I'm newbie and I'm trying to understand delegates and their use in a real scenario) In my scenario I need to...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.