Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 11-10-2007, 11:59 AM   #1 (permalink)
Lurker
 
Join Date: Nov 2003
Posts: 9
Default passiong unknown parameters at runtime

Not sure exactly how to word this, but what's the best method of passing parameters to a function in C#, when you don't know the number or type of parameters at compile time? Is this possible? The type includes class instances as well as integers, characters, floats etc.

Thanks
kock is offline   Reply With Quote
Old 11-10-2007, 02:35 PM   #2 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

Quote:
Originally Posted by kock View Post
Not sure exactly how to word this, but what's the best method of passing parameters to a function in C#, when you don't know the number or type of parameters at compile time? Is this possible? The type includes class instances as well as integers, characters, floats etc.

Thanks
You need to use reflections in order to achieve what you want.
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 11-10-2007, 03:18 PM   #3 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hic sunt leones ...
Age: 21
Posts: 1,289
Send a message via MSN to arul
Default

Code:
public void Something( params object[] args )
{
// do something with the args
}
then you can call the method like this

Code:
Something("hey", 1, new object(), "oh");
More about the 'params' keyword at MSDN: params (C#)
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell!
arul is offline   Reply With Quote
Old 11-10-2007, 03:36 PM   #4 (permalink)
Lurker
 
Join Date: Nov 2003
Posts: 9
Default

Thank you arul but that wasnīt what i was trying to do .
What i was trying to do is to invoke methods wich you donīt know their signature, parameter types and number at runtime.
In depth, i was trying to instantiate several classes invoking their constructors when they havenīt a default constructor.I used the reflection classes as daat99 said to get the constructors, their number of parameters and their types.Then i made an instance for every parameter type, wich i think itīs an ugly way to do it.Is there other cleaner way to do it?.
Code:
ConstructorInfo ctrinfo = tipo.GetConstructors();
ParameterInfo[] parinfo = ctrinfo[0].GetParameters();

if (ctrinfo.Length > 1 && parinfo[0].ParameterType == typeof(Serial))
           parinfo = ctrinfo[1].GetParameters();

object[] parametros = new object[parinfo.Length];

for (int k = 0; k < parametros.Length; k++)
           parametros[k] = Activator.CreateInstance(parinfo[k].ParameterType);

if (ctrinfo.Length > 1 && parinfo[0].ParameterType == typeof(Serial))
           item = ctrinfo[1].Invoke(parametros);
else
            item = ctrinfo[0].Invoke(parametros);
kock is offline   Reply With Quote
Old 11-10-2007, 03:49 PM   #5 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hic sunt leones ...
Age: 21
Posts: 1,289
Send a message via MSN to arul
Default

It isn't an ugly way, it's the only way. If the constructor needs certain parameters to be passed, it's so for a good reason.

Of course your code may fail on some types, but that is the price you pay for not taking care of them one by one.
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell!
arul is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5