473,800 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<TR eadFunction;
public TypeTeader(read Delegate d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
//use the ReadFunction delegate to acces the relevant function of reader
}
}

TypeReader<Int3 2Int32reader(/*pass ReadInt32 function of BinaryReader*/);

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

thanks
Colin =^.^=
Jun 27 '08 #1
7 1422
"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:l5******** ***********@new sfe2-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<TR eadFunction;
public TypeTeader(read Delegate d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
//use the ReadFunction delegate to acces the relevant function of
reader
}
}

TypeReader<Int3 2Int32reader(/*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.Reflecti on;
....
class TypeTeader<T>
{
MethodInfo ReadFunction;
public TypeReader(Meth odInfo d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
p = (T)ReadFunction .Invoke(reader, null);
}
}
....
Type t = typeof(BinaryRe ader);
TypeReader<Int3 2Int32reader = new
TypeReader<Int3 2>(t.GetMethod( "ReadInt32" ));
Jun 27 '08 #2
On Apr 23, 12:03*pm, "colin" <colin.ro...@nt world.NOSPAM.co mwrote:
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(read Delegate d)
*{
* ReadFunction=d;
*}
*public void read<T>(BinaryR eader reader,ref T p)
*{
* * //use the ReadFunction delegate to acces the relevant function of reader
*}

}

TypeReader<Int3 2Int32reader(/*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<TR eadFunction;
public readDelegate<TS etDelegate{
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.o rgwrote
in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:l5******** ***********@new sfe2-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<T ReadFunction;
public TypeTeader(read Delegate d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
//use the ReadFunction delegate to acces the relevant function of
reader
}
}

TypeReader<Int 32Int32reader(/*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.Reflecti on;
...
class TypeTeader<T>
{
MethodInfo ReadFunction;
public TypeReader(Meth odInfo d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
p = (T)ReadFunction .Invoke(reader, null);
}
}
...
Type t = typeof(BinaryRe ader);
TypeReader<Int3 2Int32reader = new
TypeReader<Int3 2>(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******** *************** ***********@26g 2000hsk.googleg roups.com...
On Apr 23, 12:03 pm, "colin" <colin.ro...@nt world.NOSPAM.co mwrote:
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<TR eadFunction;
public TypeTeader(read Delegate d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
//use the ReadFunction delegate to acces the relevant function of reader
}

}

TypeReader<Int3 2Int32reader(/*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<TR eadFunction;
public readDelegate<TS etDelegate{
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 DefualtReadDele gate(SerializeS tream file);
public static Dictionary<stri ng, DefualtReadDele gateNativeStruc ts =
GetNativeStruct s();
public DefualtReadDele gate defualtReadDele gate;
private static Dictionary<stri ng, DefualtReadDele gateGetNativeSt ructs()
{
NativeStructs = new Dictionary<stri ng, DefualtReadDele gate>();
NativeStructs.A dd("PointRegion ", delegate(Serial izeStream file)
{ PointRegion x = default(PointRe gion); if (file != null)
file.Serialize( ref x); return x; });
NativeStructs.A dd("Scale", delegate(Serial izeStream file)
{ UScale x = default(UScale) ; if (file != null) file.Serialize( ref x);
return x; });
NativeStructs.A dd("Vector", delegate(Serial izeStream file)
{ Vector x = default(Vector) ; if (file != null) file.Serialize( ref x);
return x; });
NativeStructs.A dd("Color", delegate(Serial izeStream file)
{ FColor x = default(FColor) ; if (file != null) file.Serialize( ref x);
return x; });
NativeStructs.A dd("Rotator", delegate(Serial izeStream file)
{ Rotator x = default(Rotator ); if (file != null) file.Serialize( ref x);
return x; });
NativeStructs.A dd("ADrop", delegate(Serial izeStream file)
{ ADrop x = default(ADrop); if (file != null) file.Serialize( ref x);
return x; });
return NativeStructs;
}
public UStruct(string s)
{
NativeStructs.T ryGetValue(Name , out defualtReadDele gate);
}
public ValueType Defualt(string StructName)
{
if (defualtReadDel egate != null)
return defualtReadDele gate(null);
...get custom build defualt...
}
}

public class Property
{
private void ReadStruct(Seri alizeReader file,UObject uObj,UData udata)
{//would be easier if there was a direct reference to UStruct
UStruct.Defualt ReadDelegate found;
if (UStruct.Native Structs.TryGetV alue(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.o rg>
wrote in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:l5******* ************@ne wsfe2-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(read Delegate d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
//use the ReadFunction delegate to acces the relevant function of
reader
}
}

TypeReader<In t32Int32reader(/*pass ReadInt32 function of
BinaryReade r*/);

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.Reflecti on;
...
class TypeTeader<T>
{
MethodInfo ReadFunction;
public TypeReader(Meth odInfo d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
p = (T)ReadFunction .Invoke(reader, null);
}
}
...
Type t = typeof(BinaryRe ader);
TypeReader<Int 32Int32reader = new
TypeReader<Int 32>(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.nos pamwrote in message
news:%2******** ********@TK2MSF TNGP05.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.o rg>
wrote in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>>"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:l5****** *************@n ewsfe2-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 <TReadFunctio n;
public TypeTeader(read Delegate d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
//use the ReadFunction delegate to acces the relevant function of
reader
}
}

TypeReader<I nt32Int32reader (/*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.Reflecti on;
...
class TypeTeader<T>
{
MethodInfo ReadFunction;
public TypeReader(Meth odInfo d)
{
ReadFunction=d;
}
public void read<T>(BinaryR eader reader,ref T p)
{
p = (T)ReadFunction .Invoke(reader, null);
}
}
...
Type t = typeof(BinaryRe ader);
TypeReader<In t32Int32reader = new
TypeReader<In t32>(t.GetMetho d("ReadInt32")) ;


Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2013
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 object { pClass1.m_job } )).ToString(); and it returns the error: Additional information: Object type cannot be converted to target type.
3
2328
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. Here's what I'm trying to do... I am iterating through the invocation list invoking each delegate manually. I want to eventually try to invoke a delegate to an object that has been destroyed. I'm essentially looking to get the exception...
2
1430
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 generate a new delegate class named CheckAndPrintDelegate that inherits from System.MulticastDelegate. Elsewhere it says the same delegate definition produces a delegate class inheriting from System.Delegate.
3
2619
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
5
2103
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 created. I have a delegate / event that passes via EventArgs the appropriate info from the grid to the UserControl2. This works fine, except for now I'm a little lost on the final step....I need that once the UserControl2 is "filled out" that it is...
4
3393
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 'Delegate as a valid Type declaration for the Value of a Generic Dictionary. I am curious as to why I can compile and use this syntax : it seems to me to violate the requirement that the Value of a Generic Dictionary be a Type Name.
11
1834
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 the moment i initialize the object with the constructor: public myClass(System.Timers.ElapsedEventHandler CallMeBack) {
3
7187
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 override System.Text.RegularExpression.MatchEvaluator delegate passing it another parameter. For a concrete sample I paste some lines of code : //my regexpression pattern;
7
2040
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
26
3631
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 delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even thinking. Note that since 'boxing' and 'unboxing' is involved (I think), you don't get what you...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.