Last active
December 10, 2021 19:31
-
-
Save georgekosmidis/98b0ace928db48473e44db91f35b1fdc to your computer and use it in GitHub Desktop.
These gists are part of the article: https://blog.georgekosmidis.net/2020/08/07/early-vs-late-binding-reflection-and-type-dynamic-in-csharp/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EarlyBindingSample | |
{ | |
public void RandomMethod() | |
{ | |
//... | |
} | |
} | |
var earlyBinding = new EarlyBindingSample(); | |
/***Existing method***/ | |
//This is early binding, the method is found during compile time | |
earlyBinding.RandomMethod(); | |
/***Non-Existing method***/ | |
//Method 'NotExistingMethod' does't exist: | |
earlyBinding.NotExistingMethod(); | |
//Compiler Error CS1061: | |
// 'EarlyBindingSample' does not contain a definition for 'NotExistingMethod' | |
// and no accessible extension method 'NotExistingMethod' | |
// accepting a first argument of type 'EarlyBindingSample' could be found | |
// (are you missing a using directive or an assembly reference?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ILateBoundSample | |
{ | |
void RandomMethod(); | |
} | |
//Load an assembly | |
var assembly = Assembly.LoadFile(Path.GetFullPath("path_to_your_assembly")); | |
//Locate all types that implement interface 'ILateBoundSample' | |
var types = assembly.GetTypes().Where(p => typeof(ILateBoundSample).IsAssignableFrom(p)); | |
if (types.Any()) | |
{ | |
/***Existing method***/ | |
//Create an instance of that class | |
var lateBoundSample = (ILateBoundSample)Activator.CreateInstance(types.First()); | |
//Locate and call method RandomMethod(); | |
lateBoundSample.RandomMethod(); | |
/***Non-Existing method***/ | |
//Method 'NotExistingMethod' does't exist | |
// Since 'lateBoundSample' is of type 'ILateBoundSample', compiler knows | |
lateBoundSample.NotExistingMethod(); | |
//Compiler Error CS1061: | |
// 'ILateBoundSample' does not contain a definition for 'NotExistingMethod' | |
// and no accessible extension method 'NotExistingMethod' | |
// accepting a first argument of type 'ILateBoundSample' could be found | |
// (are you missing a using directive or an assembly reference?) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LateBindingSample | |
{ | |
public void RandomMethod() | |
{ | |
//... | |
} | |
} | |
var lateBoundSample = new LateBindingSample(); | |
/***Existing method***/ | |
//Locate method 'NotExistingMethod' that doesn't exist | |
var method = lateBoundSample.GetType().GetMethod("RandomMethod"); | |
//Call method without any parameteres (null) | |
var result = method.Invoke(lateBoundSample, null); | |
/***Non-Existing method***/ | |
//Try to locate method 'RandomMethod' | |
var method = lateBoundSample.GetType().GetMethod("NotExistingMethod"); | |
//Call method without any parameteres (null) | |
var result = method.Invoke(lateBoundSample, null); | |
// You 'll get a runtime error since 'method' is null: | |
// System.NullReferenceException: 'Object reference not set to an instance of an object. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment