Given an assembly that contains
namespace Foo{public class Bar;}
How could I create an Action<Foo.Bar>
from another assembly without referencing the first assembly at compile time?
Given an assembly that contains
namespace Foo{public class Bar;}
How could I create an Action<Foo.Bar>
from another assembly without referencing the first assembly at compile time?
If you use
actionType
will now representAction<Foo.Bar>
. However, to use it, you'll need to contintue to use reflection, so you'll need to find aMethodInfo
that fits the signaturevoid(Foo.Bar)
, and callDelegate.CreateDelegate
to create a delegate. And you'll needDelegate.DynamicInvoke
to execute it.Something tells me that's not what you're thinking of...
You can't call it
Action<Foo.Bar>
in your calling code, since you won't have access to that type definition if you don't reference it at compile time. Since Delegates are contravariant, you can return anAction<Object>
and use that, or useAction<IBar>
where theIBar
interface is defined in a referenced assembly, and implemented byFoo.Bar
.If you do return an
Action<Object>
, you'd either have to useFoo.Bar
members via reflection (ordynamic
if using C# 4.0) or use cast it toFoo.Bar
where the casting code has a reference to the assembly whereFoo.Bar
is defined.