Instantiating windows form at runtime from database name C# - c#

I am having trouble instantiating a form in c# whose name i retrieve from a database, i have eliminated namespaces completely just to be sure i am not getting the object name wrong but still every time the code runs, the object returns as null instead of being the appropriate form.
private static Object CreateObjectInstance(string strObjectName)
{
Object obj = null; // Temporary object
try
{
if (strObjectName.LastIndexOf(".") == -1) // If there is no '.' in the object name
strObjectName = Assembly.GetEntryAssembly().GetName().Name + "." + strObjectName;
obj = Assembly.GetEntryAssembly().CreateInstance(strObjectName);
}
catch (Exception ex)
{
clsAdmFunctions.RecordException(ex); // Record error to the database
MessageBox.Show("Error instantiating the object\n\nDescription : "+ex.Message, "Object Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
obj = null;
}
return obj;
}
public static Form CreateForm(string strFormName)
{
return (Form)CreateObjectInstance(strFormName);
}

The problem is with your idea that assembly name is a part of your class name. True you need access to your assembly, but ultimately a class name is just Namespace.Class name. If you provide your actual namespace along with the class, then it works. Change your method to this, perhaps:
private static T CreateInstance<T>(string fullyQualifiedClassName)
{
try
{
return (T)Activator.CreateInstance(Type.GetType(fullyQualifiedClassName));
}
catch (Exception ex)
{
clsAdmFunctions.RecordException(ex); // Record error to the database
MessageBox.Show("Error instantiating the object\n\nDescription : " + ex.Message, "Object Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return default(T);
}
}
In other words you need the namespace too if you're saving it in the database. Just save class.GetType() as such or class.GetType().ToString() after which you will see the namespace too saved. The reason is you can have classes with same name, namespace1.Person and namespace2.Person in the same assembly.
If you need to read all the namespaces in an assembly, you can do:
foreach(var type in Assembly.WhateverAssembly().GetTypes())
//print type.Namespace;
You're stuck if you dont know the exact namespace. May be you can assume it to be
var namespace = Assembly.WhateverAssembly().GetTypes()[0].Namespace;
You need to have namespaces for your classes, doing otherwise is against .NET's design. If you really really want to have no namespaces for your Forms, you just need to specify just the class name, exclude the assembly name. Just call:
CreateInstance<MyForm>("MyForm");
provided MyForm is global and the assembly is just the same. If forms are in different assembly, then load it first using Assembly.Load or Assembly.LoadFrom and then create instance.

Your key method CreateObjectInstance should work OK so I am guessing it is the parameter being passed in? In my example I've shown how to include full namespace and class name etc:
namespace Example.SubFolder
{
internal class frmAdmAbout
{
public string Name { get; set; }
}
}
namespace Example.ActualApp
{
using System;
using System.Reflection;
internal class Program
{
static void Main(string[] args)
{
var newItem = CreateObjectInstance("Example.SubFolder.frmAdmAbout");
if (newItem == null)
{
Console.WriteLine("Failed to create!");
}
else
{
Console.WriteLine("Successfully created!");
}
Console.ReadKey();
}
private static Object CreateObjectInstance(string strObjectName)
{
Object obj = null;
try
{
if (strObjectName.LastIndexOf(".") == -1)
strObjectName = Assembly.GetEntryAssembly().GetName().Name + "." + strObjectName;
obj = Assembly.GetEntryAssembly().CreateInstance(strObjectName);
}
catch (Exception ex)
{
Console.WriteLine("Error instantiating the object\n\nDescription : " + ex.Message);
obj = null;
}
return obj;
}
}
}

Related

Class Level Error Handler For DAO

I am using Entity Framework. Below is an example of a list method for an Actors context in my ActorsDao class. If you imagine my application is like imdb, there will be CRUD methods for various other contexts such as Movies, Directors, Genres, Reviews, Studios etc.
Regardless of the method or context, I handle errors in the same way. Due to my many methods across many contexts, my catch section is always exactly the same.
Obviously, I could create an error handling class, put the code in there, and just call a method in that class from the catch block.
However, I'm wondering if there a way to omit the TRY...CATCH from each method and set up a global error handler for the methods in my entity framework layer?
I would only want this global error handler to handle these errors and not errors from the rest of the application.
I seem to remember in Java Spring, you could annotate a class or method with the name of a method, and all errors would be passed to that without the need of a TRY...CATCH. I'm wondering if there is something similar for .NET (or a third party library with such functionality)?
public List<Actor> ListActors()
{
List<Actor> actorList = new List<Actor>();
using (var context = new ActorContext())
{
try
{
actorList = context.Actors.ToList<Actor>();
}
catch (Exception e)
{
//Handle error code
}
}
return actorList;
}
EDIT
I did some more research and found this code from here https://stackoverflow.com/a/4851985/1753877
private void GlobalTryCatch(Action action)
{
try
{
action.Invoke();
}
catch (ExpectedException1 e)
{
throw MyCustomException("Something bad happened", e);
}
catch (ExpectedException2 e)
{
throw MyCustomException("Something really bad happened", e);
}
}
public void DoSomething()
{
GlobalTryCatch(() =>
{
// Method code goes here
});
}
Would using a delegate like this be OK? It certainly meets my requirements.
You can create a class like this and extend the controller from this class.
Error Handler class looks like this :
package com.wes.essex.rest;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.wes.essex.bean.ErrorResponse;
public class SkyNewsController {
private static final Logger LOGGER = LoggerFactory.getLogger(SkyNewsController.class);
#ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleError(Exception ex) {
LOGGER.info("start");
LOGGER.error(ex.getMessage(), ex);
ErrorResponse error = new ErrorResponse();
error.setTimestamp(ZonedDateTime.now().format(DateTimeFormatter.ISO_INSTANT));
LOGGER.debug("error : {} ", error);
ResponseEntity<ErrorResponse> response = null;
if (ex instanceof ConstraintViolationException) {
error.setReasonCode(HttpStatus.BAD_REQUEST.value());
ConstraintViolationException constraintException = (ConstraintViolationException) ex;
Set<ConstraintViolation<?>> set = constraintException.getConstraintViolations();
String errorMessage = "Input Validation Failed:";
for (ConstraintViolation<?> constraintViolation : set) {
errorMessage += constraintViolation.getMessageTemplate() + ",";
}
errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
error.setErrorMessage(errorMessage);
response = new ResponseEntity<ErrorResponse>(error, HttpStatus.BAD_REQUEST);
} else {
error.setReasonCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
error.setErrorMessage(ex.getMessage());
response = new ResponseEntity<ErrorResponse>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
return response;
}
}
This would be the baean class for error response :
package com.wes.essex.bean;
public class ErrorResponse {
private static final long serialVersionUID = 5776681206288518465L;
private String timestamp;
private String errorMessage;
private int reasonCode;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public int getReasonCode() {
return reasonCode;
}
public void setReasonCode(int reasonCode) {
this.reasonCode = reasonCode;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}

Custom Exception C#

I would like to create my own custom Exception (for my own practice), I have Man class and i would like to check the name (so its not empty, null and only English chars.
I'm not sure if I'm doing this right,
1.do i need to write the code that handles with the error (if occures) in the Custom Exception class? or in the Man's setter?
2. Where should i use the "throw new Exception" for best practice?
3. any comments\improvements about my code would be welcome.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace prog
{
class Program
{
static void Main(string[] args)
{
try
{
Man p = new Man("Dan");
}
catch (Exception e)
{
throw new NameNotValidException(e.Message);
}
}
}
class Man
{
private string name;
public string Name
{
get { return name; }
set
{
if (name == "" || name == null)
{
throw new NameNotValidException("error");
}
name = value;
}
}
public Man(string name)
{
this.name = name;
}
}
class NameNotValidException : Exception
{
public NameNotValidException()
{
Console.WriteLine("Please Write a valid name!");
}
public NameNotValidException(string message)
: base(message)
{
}
public NameNotValidException(string message, Exception inner)
: base(message, inner)
{
}
}
Thanks!
In this case it is more appropriate to throw ArgumentNullException instead. Which exception you end up using (your own or ArgumentNullException) does not matter and does not change the structure of the code below OR how you should handle an Exception.
You want to check value, not name in the setter.
Handle the exception at the calling code. If the calling code is not designed to handle the Exception then do not catch that Exception OR rethrow using throw to preserve the stack trace.
Throw the exception at the location where the code fails due to... (invalid value in this case)
Be careful with your getter/setter code, you were checking the wrong values and also bypassing the setter in the constructor in which case it would never throw an Exception to begin with.
Your Man class.
public class Man {
public Man(string name)
{
// notice capital N for Name so it is set on the property, not the field
// this will execute the setter for the Name property
this.Name = name;
}
public Man(){} // optional, but do not include the parameterized constructor you had as it sets the private fields directly OR include additional validation
private string name;
public string Name
{
get { return name; }
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("Name cannot be null or empty");
name = value;
}
}
}
Calling code which handles the exception.
try
{
// use parameterized constructor
Man p = new Man("Dan");
// or use an initializer
Man p = new Man{Name = "Dan"};
// the above initializer is actually short for
Man p = new Man();
p.Name = "Dan";
}
catch (ArgumentException e)
{
Console.WriteLine("Error occurred!! Do something...");
}
If you want to create a custom exception just extend any of the exception classes, e.g.:
class MyCustomException : System.Exception
{}
and the you can do throw new MyCustomException();
When you throw an exception you're saying "Hey, something went wrong!", so the caller can then do something about that. The exception's responsibility is to say what exactly went wrong, not how to handle it. So you should remove the Console.WriteLine("Please Write a valid name!"); from the exception. Instead, put that in the code that is actually expecting that error - i.e. your Main method.
static void Main(string[] args)
{
try
{
Man p = new Man("Dan");
}
catch (NameNotValidException e)
{
Console.WriteLine("Please Write a valid name! " + e.Message);
}
Also note that I'm using NameNotValidException in the catch block, not Exception. As a general rule you should be as specific as possible in handling errors - which is why we create custom exceptions in the first place =). For example, let's say you add an Age property, which throws an AgeNotValidException. If you catch Exception e, you'll say "Please Write a valid name!" for every error, including invalid ages. By treating every exception type separately, you can handle each error differently.
About your "throw new Exception" question, you're doing it correctly: You should throw exceptions when you are unable to do something - in this case, you are unable to set the user's name because the given name is invalid. However, you should also try and be more specific with your error messages, to make errors easier to recover from: In your case, you could change it to something along the lines of throw new NameNotValidException("Name can't be empty");, so you can tell the user not only that the name is invalid, but also exactly why.
if you want to change the message only you could use this :
throw new Exception("File check failed!");
As you need to check several types of invalid input (not empty, null and only English chars), my advice is to create the custom exception with a property for invalid input type. The example is below:
class InvalidInputCustomException : Exception
{
public string InputExceptionType { get; set; }
public InvalidInputCustomException(string inputExceptionType)
{
InputExceptionType = inputExceptionType;
}
}
Then you’ll need to create your class Man in which set accessor the input (in this code keyword value) will be checked and code lines - throw new InvalidInputCustomException .. - with corresponding input exception type in this custom exception constructor will be included. This class example is below:
class Man
{
private string _name;
public Man(string name)
{
this.Name = name;
}
public string Name
{
get { return _name; }
set
{
if (value == null)
{
throw new InvalidInputCustomException("null is not valid for input.");
}
else if (value == string.Empty)
{
throw new InvalidInputCustomException("empty is not valid for input.");
}
else
{
foreach (char ch in value)
{
if (!(ch >= 'A' && ch <= 'Z') && !(ch >= 'a' && ch <= 'z') &&
!(ch >= '0' && ch <= '9'))
{
throw new InvalidInputCustomException($"non English character {ch} is " +
$"not valid for input."); ;
}
}
}
_name = value;
}
}
}
The thrown exception must be caught in the place where to initialized Man class object its property Name is attempted to set (as for example:
p.Name = inputString
or through this object constructor as in the code example below).
The example of the Console application code is below:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter name and press key Enter:");
string inputString = Console.ReadLine();
try
{
Man p = new Man(inputString);
Console.WriteLine($"Entered name - {p.Name} - is valid.");
}
catch (InvalidInputCustomException ex)
{
Console.WriteLine($"Invalid input type - {ex.InputExceptionType}. Please enter valid name.");
}
catch (Exception ex)
{
Console.WriteLine("Unhandled exception " + ex.Message);
}
Console.WriteLine("Press any key to finish the program.");
Console.ReadLine();
}
}
The code example is more for custom exceptions understanding purposes. In real applications, you need to avoid exceptions throwing in situations related to user information entering - in this case, data validation tools must be used. During custom exception creation in a real application must be provided at least a parameterless constructor and a best practice is to add three additional constructors: with a string, with a string and an exception, for serialization.

How to create a method than can be executed on any exception in C#?

try
{
}
catch (Exception objEx)
{
clsLog.blnLogError(this.GetType().Name, MethodBase.GetCurrentMethod().Name, String.Format("Error In {0}...", MethodBase.GetCurrentMethod().Name), objEx.Message);
}
This is my Code and I need something like.
catch (MyException objEx)
{
}
class MyException
{
method()
{
//method overload with necessary parameters.
clsLog.blnLogError(this.GetType().Name, MethodBase.GetCurrentMethod().Name, String.Format("Error In {0}...", MethodBase.GetCurrentMethod().Name), objEx.Message);
}
}
In that exception class I need to get the curent class name and method name instead of writing every time.
How to achieve this?
UPDATE
[Serializable]
public class MyException : Exception
{
public MyException(string message, Exception innerException, object obj)
: base(message, innerException)
{
}
}
try
{
int f = int.Parse("d");
}
catch (MyException objEx)
{
}
It is not catching the exception and I need the method name, class name of the one where it throws error.
This cannot be done by inheriting, you will need to write an extension method so that you can call your logging method on all exception types, no matter whether they were declared by yourself or not.
To create an extension method create a static class containing a static method doing your work. Prepend the first argument of the method with the keyword this, indicating to the compiler that this method can be invoked like a member method on objects of the type of the first parameter (in your case Exception):
public static class ExceptionExtensions
{
public static void Log(this Exception ex)
{
var stackTrace = new StackTrace();
var callingMethod = stackTrace.GetFrame(1).GetMethod();
var methodName = callingMethod.Name;
var className = callingMethod.DeclaringType.Name;
clsLog.blnLogError(className, methodName, string.Format("Error In {0}...", methodName), ex.Message);
}
}
then you can call that method on every exception:
try
{
int f = int.Parse("d");
}
catch(Exception ex)
{
ex.Log();
}
For more information on extension methods see the C# Programming Guide.

Dynamically loading assemblies and instantiating types using reflection

I have a requirement to dynamically load and cast an instance of a class to it's various base types using requirement. Now on reading and trying my hand on some examples, I find that either I probably don't understand all that I need to when it comes to working with classes at runtime.
I have a situation where Abstract class B implements interface A. Now Class B is a base class for class C. When I dynamically load, at runtime, the assembly that contains all 3 types, I expect that I should be able to, using Load From context, load the assembly, create an instance of class C, and cast it to type of interface A. But that does not seem to be happening at all and I would appreciate an explanation as to why. Thanks in Advance.
http://msdn.microsoft.com/en-us/library/2xkww633.aspx
http://msdn.microsoft.com/en-us/library/1009fa28.aspx
public interface ICaseOutputGenerator
{
String SampleProperty { get; set; }
void Process();
}
public abstract class CaseOutputGeneratorBase : ICaseOutputGenerator
{
public String SecondSampleProperty { get; set; }
public virtual void SecondProcessMethod()
{
}
public abstract void ThirdSampleProcessMethod();
public string SampleProperty
{
get;
set;
}
public void Process()
{
Console.WriteLine("Process in CaseOutputGeneratorBase Called");
}
}
public class TestCaseOutputGenerator : CaseOutputGeneratorBase
{
public override void ThirdSampleProcessMethod()
{
throw new NotImplementedException();
}
}
//////////////////////////////////////////////////////////////////////////////
public class TestSandBoxManager
{
public TestSandBoxManager()
{
}
public String ProcessAssemblyByFullDisplayName(String assemblyFullDisplayName)
{
String temp = String.Empty;
var casecust = GetAssemblyByFullDisplayName(assemblyFullDisplayName);
if (casecust != null)
temp = ("Cast Passed");
else
temp = ("Cast Failed");
return temp;
}
public String ProcessFile(String assemblyName, String className)
{
String temp = String.Empty;
var casecust = GetCaseOutputGeneratorObject(assemblyName, className);
if (casecust != null)
temp=("Cast Passed");
else
temp=("Cast Failed");
return temp;
}
private static object GetAssemblyByFullDisplayName(string fullName)
{
try
{
Type caseOutputGen = null;
String fullQualifiedName = String.Empty;
var localAssembly = Assembly.LoadFrom(fullName);
foreach (var testType in localAssembly.GetTypes())
{
if ( testType.FullName != fullName)
continue;
fullQualifiedName = testType.FullName;
break;
}
if (fullQualifiedName == null)
return null;
var obj = Activator.CreateInstance(Type.GetType(fullQualifiedName));
return obj;
}
catch (Exception ex)
{
throw ex;
}
}
public String ProcessFile2(String assemblyName, String className)
{
String temp = String.Empty;
var casecust = GetCaseOutputGeneratorObjectLoadFrom(assemblyName, className);
if (casecust != null)
temp = ("Cast Passed");
else
temp = ("Cast Failed");
return temp;
}
public static ICaseOutputGenerator GetCaseOutputGeneratorObject(string assemblyName, string className)
{
ICaseOutputGenerator caseOutputGen = null;
var obj = GetObject(assemblyName, className);
if (obj != null)
caseOutputGen = (ICaseOutputGenerator)obj; // FAILS HERE
return caseOutputGen;
}
public static ICaseOutputGenerator GetCaseOutputGeneratorObjectLoadFrom(string assemblyName, string className)
{
ICaseOutputGenerator caseOutputGen = null;
try
{
var obj = GetObject2(assemblyName, className);
if (obj != null)
caseOutputGen = (ICaseOutputGenerator)obj; // FAILS HERE
}
catch (Exception ex)
{
throw ex;
}
return caseOutputGen;
}
private static object GetObject2(string fullName, string className)
{
try
{
Type caseOutputGen = null;
String fullQualifiedName = String.Empty;
var localAssembly = Assembly.LoadFrom(fullName);
foreach (var testType in localAssembly.GetTypes())
{
if (!testType.FullName.EndsWith(className, StringComparison.InvariantCultureIgnoreCase))
continue;
caseOutputGen = testType;
fullQualifiedName = testType.FullName;
break;
}
if (caseOutputGen == null)
return null;
var obj = Activator.CreateInstanceFrom(fullName, fullQualifiedName);
return obj.Unwrap();
}
catch (Exception ex)
{
throw ex;
}
}
private static object GetObject(string fullName, string className)
{
try
{
Type caseOutputGen = null;
var localAssembly = Assembly.LoadFrom(fullName);
foreach (var testType in localAssembly.GetTypes())
{
if (!testType.FullName.EndsWith(className, StringComparison.InvariantCultureIgnoreCase)) continue;
caseOutputGen = testType;
break;
}
if (caseOutputGen == null) return null;
var obj = Activator.CreateInstance(caseOutputGen);
return obj;
}
catch (FileNotFoundException ex)
{
throw new Exception("Failed to load assembly: " + Environment.NewLine + fullName, ex);
}
catch (Exception ex)
{
throw new Exception("Failed to load assembly: " + Environment.NewLine + fullName, ex);
}
}
}
//////////////////////////////////////////////////////////////////////////////
public Boolean testReflection2()
{
try
{
//create an instance of the testsandboxmanager
TestSandBoxManager t = new TestSandBoxManager();
String ret = t.ProcessFile2(#"...\Documents\visual studio 2012\Projects\TestSandBox\TestSandBox\bin\Debug\TestSandBox.dll", "TestCaseOutputGenerator");
Console.WriteLine(ret);
Console.ReadLine();
return true;
}
catch (Exception)
{
return false;
}
}
Most likely you have 2 ICaseOutputGenerator - one in each assembly. You can't cast object/interface to similarly named interface in another assembly even if code is identical. You can check the fact that create object implements ICaseOutputGenerator from its own assembly by looking at created object in the debugger.
If it is the case you need to figure out where you want to put ICaseOutputGenerator interface so it is coming from the same assembly for both "custom loaded assembly" and you main application. Often shared interfaces are implemented in separate assembly and linked to all "plug-in" assemblies as well as application assembly.
I think that Alexei Levenkov is spot on. You load your TestSandBox.dll twice. Once as a reference to your project and second time via Assembly.LoadFrom. As per documentation that you yourself linking this can result in unexpected behaviour. Here is a quote for you reference:
If an assembly is loaded with LoadFrom, and the probing path includes
an assembly with the same identity but a different location, an
InvalidCastException, MissingMethodException, or other unexpected
behavior can occur.
This is exactly what's happening in your case. If you change the path you are loading your assembly from to point to the same folder as your main executable, it will work fine.

Find class to instantiate by name without namespace or assembly? (.NET)

I'd like to instantiate a class by name (a string) without specifying a namespace or assembly. Like this (Unity syntax):
var processor = container.Resolve<IProcessor>("SpecialProcessor");
would instantiate the first IProcessor it finds called SpecialProcessor. Maybe
MyNamespace.SpecialProcessor
I'd like to avoid having to create an entry in a config every time somebody adds a new processor. I'm fine with having an entry for all candidate assemblies though.
Can I use an IoC container for something like this or should I roll my own?
Here's a function that does something very similar to what you want. You can modify it to filter based on a specific class name pretty easily.
These functions have references to a few utilities we use for logging and exception handling. You'll need to replace them with whatever you normally do in these situations.
public static T FindAndCreate<T>(bool localOnly, bool exportedOnly)
{
Type[] types = FindAssignableClasses(typeof(T), localOnly, exportedOnly, false);
if (types.Length == 0)
{
return default(T);
}
if (types.Length != 1)
{
Log.Warn(typeof(ReflectionUtil),
"FindAndCreate found {0} instances of {1} whereas only 1 was expected. Using {2}. {3}",
types.Length,
typeof(T).FullName,
types[0].FullName,
String.Join("\r\n ", Array.ConvertAll<Type, String>(types, GetFullName)));
}
try
{
return (T)Activator.CreateInstance(types[0]);
}
catch (Exception ex)
{
throw ExceptionUtil.Rethrow(ex,
"Unable to create instance of {0} found for interface {1}.",
types[0].FullName,
typeof(T).FullName);
}
}
public static Type[] FindAssignableClasses(Type assignable, bool localOnly, bool exportedOnly, bool loadDll)
{
var list = new List<Type>();
string localDirectoryName = Path.GetDirectoryName(typeof(ReflectionUtil).Assembly.CodeBase);
if (loadDll && !_loadedAllDlls)
{
foreach (string dllPath in Directory.GetFiles(localDirectoryName.Substring(6), "*.dll"))
{
try
{
Assembly.LoadFrom(dllPath);
}
catch
{
// ignore
}
}
_loadedAllDlls = true;
}
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
if (localOnly && Path.GetDirectoryName(asm.CodeBase) != localDirectoryName)
{
continue;
}
Type[] typesInAssembly;
try
{
typesInAssembly = exportedOnly ? asm.GetExportedTypes() : asm.GetTypes();
}
catch
{
continue;
}
foreach (Type t in typesInAssembly)
{
try
{
if (assignable.IsAssignableFrom(t) && assignable != t)
{
list.Add(t);
}
}
catch (Exception ex)
{
Log.Debug(
typeof(ReflectionUtil),
String.Format(
"Error searching for types assignable to type {0} searching assembly {1} testing {2}{3}",
assignable.FullName,
asm.FullName,
t.FullName,
FlattenReflectionTypeLoadException(ex)),
ex);
}
}
}
catch (Exception ex)
{
// ignore dynamic module error, no way to check for this condition first
// http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/7b02223aefc6afba/c8f5bd05cc8b24b0
if (!(ex is NotSupportedException && ex.Message.Contains("not supported in a dynamic")))
{
Log.Debug(
typeof(ReflectionUtil),
String.Format(
"Error searching for types assignable to type {0} searching assembly {1} from {2}{3}",
assignable.FullName,
asm.FullName,
asm.CodeBase,
FlattenReflectionTypeLoadException(ex)),
ex);
}
}
}
return list.ToArray();
}
It sounds like you have a plugin architecture and you want to allow other components to provide implementations of IProcessor without having to update some master config file. If that's the case, then I think you are best suited by using MEF (managed extensibility framework) (Website).
This is a framework designed to allow such behavior. Once you establish a catalog of assemblies to load components from, importing the collection of IProcessor instances is as easy as the following
var processors = container.GetExportedValues<IProcessor>();
There are many tutorials online for MEF that should get you started.

Categories

Resources