473,396 Members | 1,933 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,396 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 1404
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.