473,397 Members | 2,084 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,397 software developers and data experts.

How to get the parameter and return types of a delegate from its Type?

I have written an arithmetic expression compiler. Now that it is finished I
would like to give it a nice interface. Currently I have this interface:

delegate double Function(double x);
class ArithmeticExpression
{
public static Function Compile(string text);
...
}

This can be used like this:

Function f=ArithmeticExpression.Compile("x*x");
Console.WriteLine(f(2)); //gives 4

Obviously this only supports one parameter, which is always of type double
and called x.

I would like to do a similar interface for functions with multiple
parameters, but I do not want to have to define a delegate for each number
of parameters. So I would like to do it like this:

class ArithmeticExpression
{
public static Delegate Compile(string text,Type delegateType);
}

This could then be used like this:

delegate double Function2(double x,double y);
Function2 g=ArithmeticExpression.Compile("x*y",typeof(Functi on2));
Console.WriteLine(g(1,2));

To do it that way I need to get the number and type of parameters as well as
the return type from the delegate type. I looked at System.Type, but found
no obvious way to do this.

Is this possible? Or is there a better way to achieve a simple syntax like
the one shown above?

best regards

Rüdiger
Nov 16 '05 #1
2 1716
You might want to take a look at the params keyword in C# that allows
you to have variable number of arguments.

http://msdn.microsoft.com/library/de...clrfparams.asp

Also you can get the information regarding the params and return type
of the method by looking at the properties and methods of the MethodInfo
class which can be accessed via the Delegate.Method property

For e.g.

Type returnType = delegate.Method.ReturnType;

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

Ruediger Klaehn wrote:
I have written an arithmetic expression compiler. Now that it is finished I
would like to give it a nice interface. Currently I have this interface:

delegate double Function(double x);
class ArithmeticExpression
{
public static Function Compile(string text);
...
}

This can be used like this:

Function f=ArithmeticExpression.Compile("x*x");
Console.WriteLine(f(2)); //gives 4

Obviously this only supports one parameter, which is always of type double
and called x.

I would like to do a similar interface for functions with multiple
parameters, but I do not want to have to define a delegate for each number
of parameters. So I would like to do it like this:

class ArithmeticExpression
{
public static Delegate Compile(string text,Type delegateType);
}

This could then be used like this:

delegate double Function2(double x,double y);
Function2 g=ArithmeticExpression.Compile("x*y",typeof(Functi on2));
Console.WriteLine(g(1,2));

To do it that way I need to get the number and type of parameters as well as
the return type from the delegate type. I looked at System.Type, but found
no obvious way to do this.

Is this possible? Or is there a better way to achieve a simple syntax like
the one shown above?

best regards

Rüdiger

Nov 16 '05 #2
Sijin Joseph wrote:
You might want to take a look at the params keyword in C# that allows
you to have variable number of arguments.

http://msdn.microsoft.com/library/de...clrfparams.asp
I know this.I will probably provide a method like this as well. But
since my compiled code uses ldarg_0 etc. opcodes I can not use this
without providing a wrapper.

Also you can get the information regarding the params and return type
of the method by looking at the properties and methods of the MethodInfo
class which can be accessed via the Delegate.Method property

For e.g.

Type returnType = delegate.Method.ReturnType;

This does not do me any good since I do not have an instance of the
delegate. But I figured out how to do it just minutes after posting this
question: I just have to get the MethodInfo for the dynamically created
metohd "Invoke" using MethodInfo mi=delegateType.GetMethod("Invoke").
This methodinfo contains all the Info I need to create a matching method.
Nov 16 '05 #3

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

Similar topics

21
by: vmsgman | last post by:
Here is a code sample ... int blah = ReadFile( defArray, defFileName, w, h); // Read File Contents into memory array and return for processing public int ReadFile( ref ushort nArray, string...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
14
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm trying to understand the concept of returning functions from the enclosing functions. This idea is new to me and I don't understand when and why I would need to use it. Can someone...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
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: 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
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
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...
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
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,...
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...

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.