views:

162

answers:

2

Hi All, I really need to create something like the following, I am building 2 classes, the first is a class with the name of tableNameAsSingular (i.e AddressEntity) , in my second worker class I need to having something like the following

public IEnumerable<AddressEntity> GetAddressEntity()
{
 // the good stuff...
}

When creating the Function I have the following..

Type t = Type.GetType("IEnumerable<" + tableNameAsSingular + ">");
CodeFunction2 finderFunction = (CodeFunction2)entityServiceClass.AddFunction("Get" + table.Name, vsCMFunction.vsCMFunctionFunction, t, -1, vsCMAccess.vsCMAccessPublic, null);

but t is always null

When I do Type.GetType(tableNameAsSingular) it returns null too

Any help or pointers would be greatfully received. Also if anyone knows where a plethora of EnvDTE code generation knowledge lives I would be sooo greatful!


Update

I have now tried it just as a string using the following:

   public void AddFinderMethod()
    {
        string t = "IEnumerable<" + tableNameAsSingular + ">";
        CodeFunction2 finderFunction = (CodeFunction2)entityServiceClass.AddFunction("Get" + table.Name, vsCMFunction.vsCMFunctionFunction, t, -1, vsCMAccess.vsCMAccessPublic, null);
        // Code here remove as it does not get this far yet.
    }

but I get "IEnumerable<ProductEntity> is not a valid identifier" error message in the AddFunction Method

+3  A: 

The syntax IEnumerable<T> is C# syntax, not .NET syntax (which uses back-ticks, counters, etc). What you mean is:

Type tableType = Type.GetType(assemblyQualifiedNameToEntity);
Type enumerableType = typeof(IEnumerable<T>).MakeGenericType(tableType);

Note that Assembly.GetType is usually a better choice, as you can just use namespace-qualified names:

Assembly asm = typeof(SomeKnownType).Assembly;
Type tableType = asm.GetType(namespaceQualifiedNameToEntity);
Marc Gravell
I really appreciate your reply, When you say that Assembly.GetType is the better choice. Are you saying that the second version is the one to use, on its own?
Phill Duffy
When I do Type.GetType("Debugging.Catalog.ProductModelEntity") , I am getting null. This I expect is because it is a DSL I am working with and the code generation assembly is not the same as where the code is being generated. It is a Dynamic Type
Phill Duffy
Have you completed the dynamic type yet? Or is it still under construction? If completed, then the version starting `Assembly` should work, but the easiest option is to simply keep your dynamic Type objects in a lookup during construction. I've fought this battle - it isn't pretty ;-p
Marc Gravell
I am really struggling on how to get my Dynamic Type , any pointers? I have my CodeClass2 object
Phill Duffy
A: 

have managed to get it working with the following:

string returnType = "System.Collections.Generic.IEnumerable<" + tableNameAsSingular + ">"; 
CodeFunction2 finderFunction = (CodeFunction2)entityServiceClass.AddFunction("Get" + table.Name, vsCMFunction.vsCMFunctionFunction, returnType, -1, vsCMAccess.vsCMAccessPublic, null);
Phill Duffy