How to call custom operator with Reflection

C# compiler converts overloaded operator to functions with name op_XXXX where XXXX is the operation. For example, operator + is compiled as op_Addition. Here is the full list of overloadable operators and their respective method names: ┌──────────────────────────┬───────────────────────┬──────────────────────────┐ │ Operator │ Method Name │ Description │ ├──────────────────────────┼───────────────────────┼──────────────────────────┤ │ operator + │ op_UnaryPlus │ Unary │ │ … Read more

Passing C# parameters which can “fit” an interface, but do not actually implement it

Try to use the library Impromptu-Interface [The Impromptu-Interface] framework to allow you to wrap any object (static or dynamic) with a static interface even though it didn’t inherit from it. It does this by emitting cached dynamic binding code inside a proxy. This allows you to do something like this: var person = new Person(); … Read more

How to emit a Type in .NET Core

Here is SO post about creating a dynamic type in .NET 4. How to dynamically create a class in C#? And in the accepted answer is just one use of AppDomain. AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); Here is another SO post about a replacement of DefineDynamicAssembly function in .NET core. Is there any replace of … Read more

C# Getting Parent Assembly Name of Calling Assembly

I guess you should be able to do it like this: using System.Diagnostics; using System.Linq; … StackFrame[] frames = new StackTrace().GetFrames(); string initialAssembly = (from f in frames select f.GetMethod().ReflectedType.AssemblyQualifiedName ).Distinct().Last(); This will get you the Assembly which contains the first method which was started first started in the current thread. So if you’re not … Read more

Explicitly call static constructor

As I found out today, the static constructor CAN be called directly: from another Stackoverflow post The other answers are excellent, but if you need to force a class constructor to run without having a reference to the type (ie. reflection), you can use: Type type = …; System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle); I had to add this code … Read more

GetProperty reflection results in “Ambiguous match found” on new property

Type.GetProperty Situations in which AmbiguousMatchException occurs … …derived type declares a property that hides an inherited property with the same name, by using the new modifier If you run the following var properties = myDE.GetType().GetProperties().Where(p => p.Name == “MyEntity”); you will see that two PropertyInfo objects are returned. One for MyBaseEntity and one for MyDerivedEntity. … Read more

Difference between LoadFile and LoadFrom with .NET Assemblies?

Does this clear it up? // path1 and path2 point to different copies of the same assembly on disk: Assembly assembly1 = Assembly.LoadFrom(path1); Assembly assembly2 = Assembly.LoadFrom(path2); // These both point to the assembly from path1, so this is true Console.WriteLine(assembly1.CodeBase == assembly2.CodeBase); assembly1 = Assembly.LoadFile(path1); assembly2 = Assembly.LoadFile(path2); // These point to different assemblies … Read more