Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Equivalent Macro sub in C#.net?
Message
From
13/10/2006 11:49:31
 
 
To
12/10/2006 21:20:44
Czarina Joyce Villanueva
Innovision Systems International
Manila, Philippines
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01161629
Message ID:
01161796
Views:
10
>Is it possible to do Macro-substitution in C#.Net. This command is one of the strength of VFP. Is there any equivalent in .NET?

It's different than in VFP ... you have to use .NET's reflection capabilities. Here are some brief examples of some of what you might do with reflection:

Code to instantiate a class with reflection:
string DLLName = "MyDLL.dll";
string ClassName = "MyDll.MyClass";
System.Reflection.Assembly oDLL;
object oClass;

// This "loads" the .DLL
oDLL = System.Reflection.Assembly.LoadFrom(DLLName);

// This creates the class instance
oClass = oDLL.CreateInstance(ClassName);
And if you need to pass parameters
// Create an instance of the desired form from the assembly
object [] args = new object[1];
args[0] = MyKey;
oForm = (Form)oDLL.CreateInstance(classname, true, System.Reflection.BindingFlags.CreateInstance, null, args, null, null);
Code to set/get a property with reflection:
// Setting the MyProperty property in MyObject to ""
MyObject.GetType().InvokeMember("MyProperty", System.Reflection.BindingFlags.SetProperty, null, MyObject, new object[] {""});

// Getting the MyProperty property from MyObject:
int i = (int)MyObject.GetType().InvokeMember("MyProperty", System.Reflection.BindingFlags.GetProperty, null, MyObject, null);
There's also a type.GetProperty() method, Here's a link to Rick Strahl's blog about it:

http://west-wind.com/weblog/posts/256.aspx

He discusses using .GetProperty(). But, the above code is just as workable.
Here's the gist of it from Rick:
// Getting properties
int i = (int)this.GetType().GetProperty("MyProperty").GetValue(this, null);

// Getting Fields are a bit more complicated
Label MyLabel = (Label)this.GetType().GetField("MyLabelName", BindingFlags.Instance|BindingFlags.NonPublic).GetValue(this);
~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform