.NET Development
Late binding in C#
Gravatar is a globally recognized avatar based on your email address. Late binding in C#
  n/a
  All
  Nov 24, 2015 @ 06:25am
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

Gravatar is a globally recognized avatar based on your email address. Re: Late binding in C#
  Rick Strahl
  Nick
  Nov 24, 2015 @ 08:55am

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 ---

Gravatar is a globally recognized avatar based on your email address. Re: Late binding in C#
  n/a
  Rick Strahl
  Nov 24, 2015 @ 09:12am
Thanks Rick. I will take a look at both solutions

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



© 1996-2024