Question

I ran into a peculiarish problem. I am working on a dynamic factory project and my intention is to be able to create new objects based on an XML file. My problem is this:

  1. I have a separate project for the base classes for the factory where I have

    public abstract class DynamicContentFactory<T, Params> where T: class where Params: DynamicParameters
    
  2. In this abstract class I have a static method Create as follows

    public static T Create(Params data)
    
  3. Params only includes one string as a default called Type. I want to limit the creation of objects to the same namespace as the base class T, so I do the following:

    string namespaceStr = typeof(T).ToString();
    namespaceStr = namespaceStr.Substring(0, namespaceStr.LastIndexOf('.') + 1);
    
    Type type = Type.GetType(namespaceStr + data.Type);
    
  4. In my main project, I have a specific factory class

    public class ItemFactory : DynamicContent.DynamicContentFactory<ItemFactory, ItemParameters>
    {
    }
    
  5. The problem is that when I call ItemFactory.Create, the Type.GetType returns null. ItemFactory resides in the same namespace as the Items I want it to create, but the base class resides in another. Is there any way around this?

  6. I have tried changing the parameter of Type.GetType() to typeof(T).ToString() in order to test whether it can find it and it did not find it either. I can't create a link from my DynamicContent project to my main project because a link to the other way already exists. I feel it is silly that it cannot even find the type of the class it was initialized in.

So my question is: What would be the best way to go around this? I would prefer maintaining my DynamicContent project in a separate project as a separate library rather than having to include it in my main project. Is there a way to make it find the classes or do I have to create third project for the types I want to initialize with this in order to be able to reference it from both the main project and the DynamicContent project?

BR,

-Sami

Was it helpful?

Solution

Behavior you are observing is expected - Type.GetType

typeName - The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

Note that your current code works because it falls under "currently executing assembly" portion of the specified behavior where just namespace+name is enough.

  • You should specify full name of the type when requesting it, but you can check what namespace the type will use.
  • You can switch to Assembly.GetType instead of Type.GetType and use T's assembly to lookup types you want to create.
  • Alternative is to scan all loaded assemblies for type you want, but it may be not enough if the type is coming from not-yet-loaded assembly.

Remainder: namespace names in .Net don't mean much - they are convention to make code more readable, but there is no particular link between assembly and namespaces that are implemented in it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top