Hello,
I am using VC# 2005 ; C# 2.0 I am working on the performance
measurement tool. For that I need my code to call user defined method
along with its parameters. For that should I use a generic delegate
with variable length argument or something else ? if it is then how to
use it ?
AA
advait 8 4356
On Sat, 20 Sep 2008 05:20:14 -0700, Advait Mohan Raut
<ad*********@indiatimes.comwrote:
Hello,
I am using VC# 2005 ; C# 2.0 I am working on the performance
measurement tool. For that I need my code to call user defined method
along with its parameters. For that should I use a generic delegate
with variable length argument or something else ? if it is then how to
use it ?
Careful with that word "generic". It has a very specific meaning in C#,
and there's nothing in your post that suggests it's applicable here.
As far as the actual question goes, the Delegate class has the
DynamicInvoke() method, which takes a variable-length parameter list. So,
all delegate instances have this method that can be used when you don't
have compile-time knowledge of the signature of the method.
If that doesn't meet your needs, you'll have to be more specific about
your question, explaining why DynamicInvoke() doesn't work for you and
what exact behavior you think you need.
Pete
Peter Duniho wrote:
Careful with that word "generic". It has a very specific meaning in C#,
and there's nothing in your post that suggests it's applicable here.
As far as the actual question goes, the Delegate class has the
DynamicInvoke() method, which takes a variable-length parameter list. So,
all delegate instances have this method that can be used when you don't
have compile-time knowledge of the signature of the method.
If that doesn't meet your needs, you'll have to be more specific about
your question, explaining why DynamicInvoke() doesn't work for you and
what exact behavior you think you need.
Pete
Hello,
My class accepts a delegate from the programmer. If I make my class to
accept particual type of delegate then it will create problem when the
methos of diffrent signature is to be passed. If I make my function
just to accept general 'Delegate' then the problem is to create the
instance of the delegate which I would pass.
class TestClass
{
publicTestClass(Delegate d)
{
del=d;
}
public Call(params object[] args)
{
del.InvokeDynamic(args);
}
private Delegate del;
}
int MyMethod(int n)
{
....
}
//Main()
TestClass test=new TestClass(???); // <--- The problem is here to
pass MyMethod
AA
Advait
On Sat, 20 Sep 2008 13:09:15 -0700, Advait Mohan Raut
<ad*********@indiatimes.comwrote:
[...]
TestClass test=new TestClass(???); // <--- The problem is here to
pass MyMethod
Just pass an instance of a concrete delegate created with your method.
You can't make it simply an instance of "Delegate", since that's an
abstract class. But any other compatible delegate type will do.
If you're using .NET 3.0, you could use the Func<generic delegate type
in this particular example (and numerous other similar ones):
TestClass test = new TestClass((Func<int, int>)MyMethod);
Otherwise, you could just declare your own delegate type and use that (you
could even make it generic, just like .NET's own Func<type).
The Action<generic delegate type would be useful for methods that return
void.
I find it intriguing that C# still cannot infer an appropriate anonymous
delegate type to use in this case. It knows the signature, and it knows
the Delegate type is special. If it could, it would fix these kinds of
scenarios by making them more elegant, including one very notable one: the
Control.Invoke() method.
But for now, you need to be explicit about what type you really want (on
the bright side, once you've defined the precise type, the compiler will
at least infer the correct object instantiation for you :) ).
Pete
On Sep 21, 6:33*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sat, 20 Sep 2008 13:09:15 -0700, Advait Mohan Raut *
<advait_r...@indiatimes.comwrote:
[...]
TestClass test=new TestClass(???); *// <--- The problem is here to
pass MyMethod
Just pass an instance of a concrete delegate created with your method.
You can't make it simply an instance of "Delegate", since that's an *
abstract class. *But any other compatible delegate type will do.
If you're using .NET 3.0, you could use the Func<generic delegate type *
in this particular example (and numerous other similar ones):
* * *TestClass test = new TestClass((Func<int, int>)MyMethod);
Otherwise, you could just declare your own delegate type and use that (you *
could even make it generic, just like .NET's own Func<type).
The Action<generic delegate type would be useful for methods that return *
void.
I find it intriguing that C# still cannot infer an appropriate anonymous *
delegate type to use in this case. *It knows the signature, and it knows *
the Delegate type is special. *If it could, it would fix these kinds of*
scenarios by making them more elegant, including one very notable one: the *
Control.Invoke() method.
But for now, you need to be explicit about what type you really want (on *
the bright side, once you've defined the precise type, the compiler will *
at least infer the correct object instantiation for you :) ).
Pete
Thank you Peter Duniho for the response.
Since I use C# 2.0 , I don't have any other way except creating the
signatures for the functions.
On Sun, 21 Sep 2008 10:57:10 -0700, Advait Mohan Raut
<ad*********@indiatimes.comwrote:
Thank you Peter Duniho for the response.
Since I use C# 2.0 , I don't have any other way except creating the
signatures for the functions.
Note that using C# 2.0 isn't necessarily mutually exclusive with using
..NET 3.0. Though, the default toolset does make it that way.
In any case, like I said, you can declare the generic delegate types
yourself. There's nothing special about them. They just happen to be
built into .NET. So you don't necessarily need to declare delegates for
every possible signature. You can just declare the needed generic
delegates and use them as appropriate:
delegate void Action();
delegate void Action<T>(T t);
delegate void Action<T1, T2>(T1 t1, T2 t2);
// etc.
delegate TResult Func<TResult>();
delegate TResult Func<T, TResult>(T t);
delegate TResult Func<T1, T2, TResult>(T1 t1, T2 t2);
// etc.
Pete
Peter Duniho <Np*********@nnowslpianmk.comwrote:
If you're using .NET 3.0, you could use the Func<generic delegate type
in this particular example (and numerous other similar ones):
TestClass test = new TestClass((Func<int, int>)MyMethod);
Otherwise, you could just declare your own delegate type and use that (you
could even make it generic, just like .NET's own Func<type).
The Action<generic delegate type would be useful for methods that return
void.
Just a slight correction - these types were introduced in .NET 3.5, not
..NET 3.0.
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
On Sun, 21 Sep 2008 22:47:32 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Just a slight correction - these types were introduced in .NET 3.5, not
.NET 3.0.
Yeah, I always get that mixed up. Especially since Action<Thas been
with us since .NET 2.0, it's hard to keep track.
Oh well...either way, it's probably easier to just define them oneself
than to mess around with getting C# 2.0 working with .NET 3.5. :)
I find it intriguing that C# still cannot infer an appropriate anonymous
delegate type to use in this case
...
including one very notable one: the
Control.Invoke() method.
So true; a real frustration at times. As an aside (and not helpful for
Advait with C# 2.0), one option with Control.Invoke is an extension
method:
public static void Invoke(this Control control, Action action)
{ // note: could use MethodInvoker for 2.0 compartibility
(with ExtensionAttribute)
if (control == null) throw new
ArgumentNullException("control");
if (action == null) throw new
ArgumentNullException("action");
control.Invoke(action, null);
}
then you can use:
this.Invoke(() =this.Text = "Hi");
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Isaac |
last post by:
Hello,
I'm using the Generic List class in System.Collection.Generics to
implement a strongly-typed list of my own class. I want to implement the
Find methods and have to use the Predicate...
|
by: Walter L. Preuninger II |
last post by:
I would like to write a generic procedure that will take string or numeric
variables. I can not think of a way to make this more clear except to show
what I want.
int main(void)
{
int i=7;...
|
by: Jon Davis |
last post by:
The garbage handler in the .NET framework is handy. When objects fall out of
scope, they are automatically destroyed, and the programmer doesn't have to
worry about deallocating the memory space...
|
by: Aquila Deus |
last post by:
Hi all!
I found a problem when using generic with delegate:
delegate RT MethodTemplate <RT> ();
delegate RT MethodTemplate <RT, AT0> (AT0 a0);
delegate RT...
|
by: David Veeneman |
last post by:
I have several events that pass a value in their event args. One event
passes an int, another a string, another a DateTime, and so on. Rather than
creating a separate set of event args for each...
|
by: Martin Carpella |
last post by:
Hello everyone,
I experience some strange behaviour of anoynmous delegates which refer
to variables outside the scope of the delegate.
Please have a look at the following code and output at...
|
by: Bill Woodruff |
last post by:
I've found it's no problem to insert instances of named delegates as values
into a generic dictionary of the form :
private Dictionary<KeyType, DelegatemyDictionary = new
Dictionary<KeyType,...
|
by: Dave |
last post by:
I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegate;
instantiation:...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |