Connecting Tech Pros Worldwide Forums | Help | Site Map

CallByName in C#????

Jelle van Baardewijk
Guest
 
Posts: n/a
#1: Nov 15 '05
Does anybody know if there's way to do this in C#?

Jelle van Baardewijk


Miha Markic
Guest
 
Posts: n/a
#2: Nov 15 '05

re: CallByName in C#????


Hi,

What?
Calling a method by its name?
Yes, it is possible - see reflection's MethodInfo class.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Jelle van Baardewijk" <jvb@databalk.nl> wrote in message
news:%23c6Ml6n3DHA.2428@tk2msftngp13.phx.gbl...[color=blue]
> Does anybody know if there's way to do this in C#?
>
> Jelle van Baardewijk
>
>[/color]


Lawrence Oluyede
Guest
 
Posts: n/a
#3: Nov 15 '05

re: CallByName in C#????


"Jelle van Baardewijk" <jvb@databalk.nl> writes:
[color=blue]
> Does anybody know if there's way to do this in C#?[/color]

What is "callbyname" ?

--
Lawrence "Rhymes" Oluyede
http://loluyede.blogspot.com
phoenix
Guest
 
Posts: n/a
#4: Nov 15 '05

re: CallByName in C#????


Reflection is one way to do it, but you can also use the VB version by
simply adding a reference to the visual basic namespace.

Yves

"Jelle van Baardewijk" <jvb@databalk.nl> schreef in bericht
news:%23c6Ml6n3DHA.2428@tk2msftngp13.phx.gbl...[color=blue]
> Does anybody know if there's way to do this in C#?
>
> Jelle van Baardewijk
>
>[/color]


Patrick Steele [MVP]
Guest
 
Posts: n/a
#5: Nov 15 '05

re: CallByName in C#????


In article <87d69grwsq.fsf@mobile.foo>, raims@dot.com says...[color=blue]
> "Jelle van Baardewijk" <jvb@databalk.nl> writes:
>[color=green]
> > Does anybody know if there's way to do this in C#?[/color]
>
> What is "callbyname" ?[/color]

It's from VB6. It allows you to execute methods on an object based on
the "name" of the method. It's basically late-binding, but the method
name may not be known at design-time. The code determines the method to
call and uses the CallByName function to dynamically make the call.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Garrett
Guest
 
Posts: n/a
#6: Nov 15 '05

re: CallByName in C#????


Call by name has to do with the way arguments
are passed, not calling a method by it's name. :-)

See for example:
http://wombat.doc.ic.ac.uk/foldoc/fo...i?call-by-name


"Miha Markic" <miha at rthand com> wrote in message
news:O8R6VBo3DHA.1948@TK2MSFTNGP12.phx.gbl...[color=blue]
> Hi,
>
> What?
> Calling a method by its name?
> Yes, it is possible - see reflection's MethodInfo class.
>
> --
> Miha Markic - RightHand .NET consulting & software development
> miha at rthand com
> www.rthand.com
>
> "Jelle van Baardewijk" <jvb@databalk.nl> wrote in message
> news:%23c6Ml6n3DHA.2428@tk2msftngp13.phx.gbl...[color=green]
> > Does anybody know if there's way to do this in C#?
> >
> > Jelle van Baardewijk
> >
> >[/color]
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#7: Nov 15 '05

re: CallByName in C#????


Garrett <garrett_vanmeterNOSPAM@NOSPAMhotmail.com> wrote:[color=blue]
> Call by name has to do with the way arguments
> are passed, not calling a method by it's name. :-)
>
> See for example:
> http://wombat.doc.ic.ac.uk/foldoc/fo...i?call-by-name[/color]

That may be true in Computer Science, but I don't believe it's what was
meant by the OP...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Lawrence Oluyede
Guest
 
Posts: n/a
#8: Nov 15 '05

re: CallByName in C#????


Patrick Steele [MVP] <patrick@mvps.org> writes:
[color=blue][color=green]
>> What is "callbyname" ?[/color]
>
> It's from VB6. It allows you to execute methods on an object based on
> the "name" of the method. It's basically late-binding, but the method
> name may not be known at design-time. The code determines the method to
> call and uses the CallByName function to dynamically make the call.[/color]

Yeah, thanks, it seems that reflection can handle this sort of things, right?

--
Lawrence "Rhymes" Oluyede
http://loluyede.blogspot.com
Patrick Steele [MVP]
Guest
 
Posts: n/a
#9: Nov 15 '05

re: CallByName in C#????


In article <87r7xvoliq.fsf@mobile.foo>, raims@dot.com says...[color=blue]
> Patrick Steele [MVP] <patrick@mvps.org> writes:
>[color=green][color=darkred]
> >> What is "callbyname" ?[/color]
> >
> > It's from VB6. It allows you to execute methods on an object based on
> > the "name" of the method. It's basically late-binding, but the method
> > name may not be known at design-time. The code determines the method to
> > call and uses the CallByName function to dynamically make the call.[/color]
>
> Yeah, thanks, it seems that reflection can handle this sort of things, right?[/color]

Yup!

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Carsten Posingies
Guest
 
Posts: n/a
#10: Nov 15 '05

re: CallByName in C#????


Patrick Steele [MVP] wrote:[color=blue]
> In article <87r7xvoliq.fsf@mobile.foo>, raims@dot.com says...[color=green]
>> Patrick Steele [MVP] <patrick@mvps.org> writes:
>>[color=darkred]
>>>> What is "callbyname" ?
>>>
>>> It's from VB6. It allows you to execute methods on an object based
>>> on the "name" of the method. It's basically late-binding, but the
>>> method name may not be known at design-time. The code determines
>>> the method to call and uses the CallByName function to dynamically
>>> make the call.[/color]
>>
>> Yeah, thanks, it seems that reflection can handle this sort of
>> things, right?[/color]
>
> Yup![/color]

Just for couriousity, could you point me to an example in C#?


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#11: Nov 15 '05

re: CallByName in C#????


Carsten Posingies <c.posingies@gmx.de> wrote:[color=blue]
> Just for couriousity, could you point me to an example in C#?[/color]

Yup:

using System;
using System.Reflection;

class Test
{
string name;

public Test (string name)
{
this.name=name;
}

public void PrintGreeting(string other)
{
Console.WriteLine ("{0} greets {1}", name, other);
}

static void Main()
{
Test t = new Test("Jon");
MethodInfo method = typeof(Test).GetMethod("PrintGreeting");

method.Invoke (t, new object[]{"Carsten"});
}
}

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Carsten Posingies
Guest
 
Posts: n/a
#12: Nov 15 '05

re: CallByName in C#????


Jon Skeet [C# MVP] wrote:[color=blue]
> Carsten Posingies <c.posingies@gmx.de> wrote:[color=green]
>> Just for couriousity, could you point me to an example in C#?[/color]
>
> Yup:[/color]
[color=blue]
> MethodInfo method = typeof(Test).GetMethod("PrintGreeting");
>
> method.Invoke (t, new object[]{"Carsten"});[/color]

Argh! *blush* Same as one can call a JScript's method from within C#
code when tampering with the script engine.

If there was a way to "compound" GetMethod() and Invoke(), we would have
had something similar to JScript's eval()... If there wasn't the word
"if"... <g>

Thanks!


Jelle van Baardewijk
Guest
 
Posts: n/a
#13: Nov 15 '05

re: CallByName in C#????


Thnx Guys,

This is what I needed:

public static void SetControlProperty(Control control, string propertyName,
object propertyValue)
{
Type myType = control.GetType();
PropertyInfo myInfo = myType.GetProperty(propertyName);
myInfo.SetValue(control, propertyValue, null);
}


"Carsten Posingies" <c.posingies@gmx.de> schreef in bericht
news:OgcSyl13DHA.2404@TK2MSFTNGP12.phx.gbl...[color=blue]
> Jon Skeet [C# MVP] wrote:[color=green]
> > Carsten Posingies <c.posingies@gmx.de> wrote:[color=darkred]
> >> Just for couriousity, could you point me to an example in C#?[/color]
> >
> > Yup:[/color]
>[color=green]
> > MethodInfo method = typeof(Test).GetMethod("PrintGreeting");
> >
> > method.Invoke (t, new object[]{"Carsten"});[/color]
>
> Argh! *blush* Same as one can call a JScript's method from within C#
> code when tampering with the script engine.
>
> If there was a way to "compound" GetMethod() and Invoke(), we would have
> had something similar to JScript's eval()... If there wasn't the word
> "if"... <g>
>
> Thanks!
>
>[/color]


Closed Thread


Similar C# / C Sharp bytes