Does anyone out there have a good working example you could share with me. Your help would be most appreciative
Thanks
Nick
You can use dynamic in C# which allow you to late bind to anything at runtime. Or you can use Reflection.
Using dynamic:
var type = GetTypeFromName("namespace.sometype");
dynamic obj = Activator.CreateInstance(type) as dynamic;
*** Not strongly typed
string stringVal = obj.SomeProperty;
dynamic result = obj.SomeMethod("Parm1,"Parm2",2);
Console.Log(result);
Reflection is a bit more involved, but you can use some easy helpers. I have Westwind.Utilities (which you can install from NuGet) which includes a ReflectionUtils class. Using that you can do:
object obj = ReflectionUtils.CreateInstanceFromString("namespace.sometype")
string stringVal = ReflectionUtils.GetProperty(obj,"SomeProperty")
object result = ReflectionUtils.CallMethod(obj,"SomeMethod","Parm1","Parm2",2);
Westwind.Utilities on GitHub (part of WestwindToolkit): https://github.com/RickStrahl/WestwindToolkit/tree/master/Westwind.Utilities
Westwind.Utilities on Nuget (for loading into .NET projects): https://www.nuget.org/packages/Westwind.Utilities/
Hope this helps,
+++ Rick ---
Have a great Thanksgiving
Nick
You can use dynamic in C# which allow you to late bind to anything at runtime. Or you can use Reflection.
Using dynamic:
var type = GetTypeFromName("namespace.sometype");
dynamic obj = Activator.CreateInstance(type) as dynamic;*** Not strongly typed
string stringVal = obj.SomeProperty;
dynamic result = obj.SomeMethod("Parm1,"Parm2",2);
Console.Log(result);
Reflection is a bit more involved, but you can use some easy helpers. I have Westwind.Utilities (which you can install from NuGet) which includes a ReflectionUtils class. Using that you can do:
object obj = ReflectionUtils.CreateInstanceFromString("namespace.sometype")string stringVal = ReflectionUtils.GetProperty(obj,"SomeProperty")
object result = ReflectionUtils.CallMethod(obj,"SomeMethod","Parm1","Parm2",2);
Westwind.Utilities on GitHub (part of WestwindToolkit):
https://github.com/RickStrahl/WestwindToolkit/tree/master/Westwind.Utilities
Westwind.Utilities on Nuget (for loading into .NET projects):
https://www.nuget.org/packages/Westwind.Utilities/
Hope this helps,
+++ Rick ---
In VFP late binding to an object is the norm and is quite simple to do. I am finding it much more difficult to do in.Net with C# 6.
Does anyone out there have a good working example you could share with me. Your help would be most appreciative
Thanks
Nick