Jump to content



Photo
c# .net

  • Please log in to reply
10 replies to this topic

#1 wrack

wrack

    Wireless Robotic Android Calibrated for Killing

  • Joined: 09-December 06
  • Location: Melbourne, Australia

Posted 04 October 2012 - 04:30

Hi,

Been using the code below to load a dll dynamically and get an instance of certain interface. This was done way back in .NET 2 days. What is a better & more efficient way to do this using .NET 4 functionality?

// Usage
ICountryHandler pCountryHandler = GetInstanceFromModule<ICountryHandler>("Company.Product.Country.Australia.dll");

public static TInterface GetInstanceFromModule<TInterface>(string AModuleName)
where TInterface : class
{
Assembly pAssembly;
ConstructorInfo pConstructor;
TInterface pInstance;
Type pInterface;
pAssembly = null;
if (AModuleName.Contains(Path.DirectorySeparatorChar.ToString()) || AModuleName.Contains(Path.AltDirectorySeparatorChar.ToString()) || AModuleName.Contains(Path.PathSeparator.ToString()) || AModuleName.Contains(Path.VolumeSeparatorChar.ToString()))
{
  pAssembly = Assembly.LoadFrom(AModuleName);
}
else
{
  pAssembly = Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), AModuleName));
}
if (pAssembly != null)
{
  foreach (Type pType in pAssembly.GetExportedTypes())
  {
   pInterface = pType.GetInterface(typeof(TInterface).ToString());
   if (pInterface != null)
   {
    pConstructor = pType.GetConstructor(new Type[] { });
    pInstance = pConstructor.Invoke(null) as TInterface;
    if (pInstance != null)
    {
	 return pInstance;
    }
   }
  }
}
return null;
}

TA :)


#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 04 October 2012 - 07:34

pInterface = pType.GetInterface(typeof(TInterface).ToString());
if (pInterface != null)
I take it this tests if pType implements TInterface? I'd do:
if (typeof(TInterface).IsAssignableFrom(pType))

Also rather than getting the default constructor and invoking explicitely, you could use Activator for that:

pConstructor = pType.GetConstructor(new Type[] { });
pInstance = pConstructor.Invoke(null) as TInterface;

// instead do
pInstance = (TInterface)Activator.CreateInstance(pType);

No need to use as and check for null since you just checked that pType implements TInterface: the cast must necessarily succeed.

None of this is .NET 4-specific but might allow you to cut down the number of lines a bit.

#3 Aethec

Aethec

    Neowinian Senior

  • Joined: 02-May 10

Posted 04 October 2012 - 07:46

You should divide that method into smaller methods - one to get the assembly from a string, one to get all types implementing an interface, and one to create an instance of all these types.
Your check for absolute path can be replaced with Path.IsPathRooted().

Also, stop using Hungarian notation...especially if you're not actually using the prefix but just using "p" at the beginning of everything. It's both ugly and useless. ( see http://msdn.microsof...o/ms229045.aspx - "Do not use Hungarian notation").

#4 +GreenMartian

GreenMartian

    Neowinian Senior

  • Joined: 28-August 04
  • Location: adelaide, au

Posted 04 October 2012 - 07:51

Also, you might be able to Linq-ify the foreach + type check and everything in it - if you really want to try something new :)

#5 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 04 October 2012 - 07:58

Actually the biggest efficiency gains here would be by caching the results. Iterating all the types in an assembly can take a long time and you're doing it every time the method is called. Of course, measure your gains - how much time this method currently takes and how much you're saving (or wasting!) by doing things differently. You might find out this method takes an insignificant amount of time and you're just worrying for nothing.

#6 +GreenMartian

GreenMartian

    Neowinian Senior

  • Joined: 28-August 04
  • Location: adelaide, au

Posted 04 October 2012 - 08:45

Iterating all the types in an assembly can take a long time and you're doing it every time the method is called.

(Y)
Unless it's a once-off thing - then it's probably not worth overcomplicating it.

And just for fun, here's a LINQ version:

(PS: If you often need to test code snippets (not necessarily LINQ-related), LINQPad is invaluable)

public static TInterface GetInstanceFromModule<TInterface>() where TInterface : class
{
  // Change this to load assembly from file
  var pAssembly = Assembly.GetAssembly(typeof(TInterface));

  // Magic!
  if (pAssembly != null)
	return pAssembly.GetExportedTypes()
	 .Where(t => t.IsClass)
	 .Where(t => typeof(TInterface).IsAssignableFrom(t))
	 .Select(t => (TInterface)Activator.CreateInstance(t))
	 .FirstOrDefault();

  return null;
}

Edit: It doesn't really increase efficiency, but does make it a lot easier on the eyes. And slightly harder to debug :p

Edited by GreenMartian, 04 October 2012 - 08:51.


#7 OP wrack

wrack

    Wireless Robotic Android Calibrated for Killing

  • Joined: 09-December 06
  • Location: Melbourne, Australia

Posted 04 October 2012 - 09:49

Thanks guys.

I am using hungarian notations as that is the coding practices where I work and I gotta follow their guidelines. I am going to implement few of these suggestions and see how things go. I am only calling this once for each DLL that I want to load and once they are loaded that is it.

I read somewhere to use newly introduced dynamic key word and use managed extensibility framework but I am stuck there... Any advice on that would be useful too.

Cheers :)

#8 +GreenMartian

GreenMartian

    Neowinian Senior

  • Joined: 28-August 04
  • Location: adelaide, au

Posted 04 October 2012 - 11:14

Ah, didn't realise MEF is now built into .NET 4.

From your example, it looks like you're looking more into Dependency Injection (DI). From what I remember, MEF does it to some extent, but is more geared towards supporting extensions (I haven't actually used it, but did some reading on it a couple years ago, so feel free to correct me).

There are multiple frameworks that provide DI Containers. Personally, I've only used Castle Windsor. There's also Unity, and a few others.
Setup properly, DI containers make it ridiculously easy to inject interface implementations to other classes.

In a nutshell, you basically tell it once, at the start of your app, to load a config from an xml file like so:
container.Install(Configuration.FromXmlFile("settings.xml")

With the xml file looking something like
<configuration>
<components>
  <component service="Namespace.ICountryHandler, AssemblyNameOfInterface"
	type="Namespace.AustralianHandler, AssemblyNameOfImplementor" />
</components>
</configuration>

And everywhere else in the code when you have a property of type ICountryHandler, you'll get the Australian handler.

PS: Thanks for mentioning MEF. I might revisit it and see what MS have been up to. There goes my weekend!

#9 OP wrack

wrack

    Wireless Robotic Android Calibrated for Killing

  • Joined: 09-December 06
  • Location: Melbourne, Australia

Posted 04 October 2012 - 11:16

Thanks for the heads up. Please do post your findings here. Trouble here is lack of time even on weekends.

#10 +GreenMartian

GreenMartian

    Neowinian Senior

  • Joined: 28-August 04
  • Location: adelaide, au

Posted 04 October 2012 - 11:42

Thanks for the heads up. Please do post your findings here. Trouble here is lack of time even on weekends.

Shoulda done over the long weekend last week :p

#11 OP wrack

wrack

    Wireless Robotic Android Calibrated for Killing

  • Joined: 09-December 06
  • Location: Melbourne, Australia

Posted 04 October 2012 - 12:17

Shoulda done over the long weekend last week :p

Labour Day I guess. Here in Victoria we have it in March.