I am trying to reorganize a piece of code and have exhausted the options that I can come up with.
This is the original code I had:
if (WebConfigSettings.ComPartition == null && HttpContext.Current != null) Nses = new NSession(); else Nses = (INSession)Marshal.BindToMoniker(string.Format("partition:{0}/new:NuntioServer.NSession", WebConfigSettings.ComPartition));
AND
if (WebConfigSettings.ComPartition == null && HttpContext.Current != null) apses.Wses = new WSession(); else apses.Wses = (IWSession)Marshal.BindToMoniker(string.Format("partition:{0}/new:NuntioServer.WSession", WebConfigSettings.ComPartition));
And here is how I am trying to reorganize this:
(Yes, in C # you can instantiate an interface .)
public static TInterface Get<TSubInterface, TInterface>() where TSubInterface: TInterface { <snip></snip> if (!useComPartitions) return Activator.CreateInstance<TSubInterface>();
Here is what I have already tried:
I tried to specify the restriction new (), and then make "new TSubInterface ()": this leads to a build error: "... there must be a non-abstract type with an open constructor without parameters in order to use it as the parameter" TSubInterface "in the generic type or method. "
when I use Activator.CreateInstance, I get an exception at runtime: "Unable to instantiate interface"
when I use Activator.CreateComInstanceFrom ("someAssemblyName", "typeName"), I get a compilation error: "Unable to convert the expression type" System.Runtime.Remoting.ObjectHandle "to return a TInterface type"
[edit] I was able to make this compiler by adding 'where is TSubInterface: class, but I'm not sure if this makes sense, since TSubInterface is an interface.
Using CreateComInstanceFrom also does not work, as it tries to find the assembly specified in the directory where this DLL is not and should not be.
Is there any way to do this compilation and run?
generics c # activator com
TweeZz Sep 28 2018-12-12T00: 00Z
source share