| | |
Dynamically Reference Member Variable
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
I have a function which initiates commands for a command class. Since there are many commands to initiate, writing code for each one would be tedious.
The function definition is as follows-
Now, my problem is that I wish to reference the commands dynamically. I pass in the command name, it references the public static RoutedUICommand variable.
In PHP, the equivalent would be-
I can't figure out how this would be done in C#.
The function definition is as follows-
C# Syntax (Toggle Plain Text)
private static void InitCommand(KeyGesture input, String text, String name) { InputGestureCollection inputs = new InputGestureCollection(); inputs.Add(input); Type t = typeof(UploadCommands); t.GetMember(name); (RoutedUICommand)t.GetMember(name) = new RoutedUICommand(text, name, typeof(UploadCommands), inputs); }
Now, my problem is that I wish to reference the commands dynamically. I pass in the command name, it references the public static RoutedUICommand variable.
In PHP, the equivalent would be-
PHP Syntax (Toggle Plain Text)
$class->$member = "Blah, blah";
I can't figure out how this would be done in C#.
Last edited by Ishbir; Nov 4th, 2009 at 11:04 am.
0
#2 Nov 4th, 2009
Are you referring to a class field in C#?
Then use property syntax to access it.
Your code looks a bit strange, why are you making a inputs collection and just put one item input into it?
Then use property syntax to access it.
Your code looks a bit strange, why are you making a inputs collection and just put one item input into it?
Last edited by ddanbe; Nov 4th, 2009 at 12:02 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
1
#3 34 Days Ago
Take a look at this article - http://www.c-sharpcorner.com/UploadF...ionin.NET.aspx
•
•
Join Date: Jun 2009
Posts: 442
Reputation:
Solved Threads: 82
2
#4 34 Days Ago
•
•
•
•
In PHP, the equivalent would be-
PHP Syntax (Toggle Plain Text)
$class->$member = "Blah, blah";
I can't figure out how this would be done in C#.
.NET have this feature. It is called Reflection in .NET.
You can use reflection to dynamically create an instance of a type(class), bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
Refer this link and also the link given by adatapost.
1
#5 34 Days Ago
You may also consider using an enumeration member to define all of your commands. This way you have a strongly typed way to reference the command. You can call
Results in:
You could then go on to design an attribute and use the enumeration member in your attribute for the class member that represents the command.
.ToString() on the enumeration member and call Enum.Parse() : C# Syntax (Toggle Plain Text)
internal enum CommandTypes { Command1 = 1, Command2 = 2 } private void button2_Click(object sender, EventArgs e) { CommandTypes cmd = CommandTypes.Command2; string sCmdString = cmd.ToString(); Console.WriteLine("cmd string value: " + sCmdString); Console.WriteLine("cmd int value: " + ((int)cmd).ToString()); CommandTypes parsedCmd = (CommandTypes)Enum.Parse(typeof(CommandTypes), sCmdString); System.Diagnostics.Debugger.Break(); }
Results in:
text Syntax (Toggle Plain Text)
cmd string value: Command2 cmd int value: 2
You could then go on to design an attribute and use the enumeration member in your attribute for the class member that represents the command.
![]() |
Similar Threads
- How do i create a pointer to a linked list as a member variable? (C++)
- Problem on Returning Reference to Template Object (C++)
- what is a reference parameter and how would i go about this code? (C++)
- An equivalent to a vector<ifstream> member variable (C++)
- const reference variable declaration more efficient ? (C++)
- Using OpenGL in Visual C++: Part I (Game Development)
- using hash in perl objects (Perl)
- new member here :) (C)
Other Threads in the C# Forum
- Previous Thread: Saving and Discarding mails
- Next Thread: UDP Port Forwarding
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox concurrency control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ globalization httpwebrequest image index input install java label list listbox listener mandelbrot math microsoftc#visualexpress mouseclick mysql networking operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






