A library to cast one object to an interface, from which it doesn't inherit.
- You're hacking around a lot, and need to work with IL emitting code
- You're loading in assemblies with your own
AssemblyLoadContext
, you've messed up the load dependencies
In general, you'll have an interface and an object that you wish to cast to that interface. The ReflectedCast can be used like this. Notice how the class TestClass
does not inherit from ITestInterface
.
class Program
{
static void Main(string[] args)
{
TestClass orig = new TestClass();
ITestInterface asInterface = ReflectedCaster.Default.CastToInterface<ITestInterface>(orig);
Console.WriteLine(asInterface.Method());
}
}
public interface ITestInterface
{
string Method();
}
public class TestClass
{
public string Method()
{
return "Hello world";
}
}
- You can use
ReflectedCaster.Default
or create a newReflectedCaster
with certain options. - Once you have a
ReflectedCaster
, you can useCastToInterface()
to do the casting. Here you can also specify more options for your cast - All involved interfaces, classes and methods / properties / events .., MUST be public. They will be invoked from a new assembly, and need to be acessible.
Option | Default | Description |
---|---|---|
SupportExplicitImplementations | true |
Supports the case of explicitly implemented interfaces. Priority is given to explicit implementations that have the samme interface.FullName as the interface you're Converting to |
SupportExplicitImplementationsByInterfaceName | true |
Supports the case of explicitly implemented interfaces. Unlike SupportExplicitImplementations , matching is only done on interface.Name of the interface you're Converting to |
SupportSpecificReturnTypes | false |
Support the cases where an interface defines a more generic return type, than the one the instance returns. Example, interface: object Method(); , class: string Method(); |
Option | Default | Description |
---|---|---|
AllowMissingFunctions | false |
Allow cases where an instance is missing one or more functions defined in the interface . The functions will throw an exception when called |