Visual Studio Development Bookmark and Share   
 index > Visual Studio Extensibility > EnvDte CodeFunction IEnumerable
 

EnvDte CodeFunction IEnumerable

Hi All, I really need to create something like 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, as this code is being generate the tableNameAsSingular type will not exist until just before this CodeFuncation is added.

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!

Best Regards,

Phill
skype: phill.duffy
BDC Meta Man Web Edition - Out Now
Phill Duffy [MCTS]  Monday, October 12, 2009 2:39 PM
I have managed to get it working with the following:

string returnType = "System.Collections.Generic.IEnumerable<" + tableNameAsSingular + ">"; <br />
CodeFunction2 finderFunction = (CodeFunction2)entityServiceClass.AddFunction("Get" + table.Name, vsCMFunction.vsCMFunctionFunction, returnType, -1, vsCMAccess.vsCMAccessPublic, null);
           
Why Chao's worked without the full name is of wonder but heck, I am a happy bunny!!!!!!!!!!!!!

BDC Meta Man Web Edition - Out Now
Phill Duffy [MCTS]  Wednesday, October 14, 2009 1:50 PM

Hi, Phill
I am doubting why you not use the type string directly, the Type parameter is a object type and could receive a string As its value. Like

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

I think this is perfect.
If I misunderstood you, feel free to let me know

Thanks

Chao 

Chao Kuo  Tuesday, October 13, 2009 4:02 AM
Hi Chao,

Thank you for your reply, I did have it like you do there, with the string, but I get the following:

"IEnumerable<CustomerEntity>' is not a valid identifier"

I create these classes in the DSL I am writing:

1: CustomerEntity - This defines Customer properties
2: CustomerEntityService - This has methods for processing CustomerEntities

When an item is dropped onto my DSL diagram it creates the files in the users solution, if I could get the 'Dynamic Type' from the CodeClass2 I might be able to use that but so far I have been unable to find out how to do this.

I am working with a DSL which means my CodeGenerator class sits in a different assembly/solution as to where this code is being generated. I am able to get IntelliSense on the CustomerEntity class, if I go in and manually type into the CustomerEntityService.cs

Regards,

Phill






BDC Meta Man Web Edition - Out Now
Phill Duffy [MCTS]  Tuesday, October 13, 2009 8:52 AM
Hi, Phill
Thanks for your following up.
The Type parameter does not receive a Type object, and it is not right for you to get type through
Type t = Type.GetType("IEnumerable<" + tableNameAsSingular + ">");
Expression. If you want to use IEnumerable<CustomerEntity> as your return type, I think the most appropriate method is use string as the parameter.
I have test the expression, it does not throw any errors on my side if the CustomerEntity could be recognized.
And could you post the fragment here in order for me to find the error.
Thanks
Chao
Chao Kuo  Tuesday, October 13, 2009 10:14 AM
Hi Chao, Thanks again for your reply:

       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.
        }
'IEnumerable<ProductEntity>' is not a valid identifier

   at Microsoft.VisualStudio.CSharp.Services.Language.BaseException.ThrowEINVALIDARG(ILangService iLangService, jrc_StringResource_identifiers stringId, Object[] args)
   at Microsoft.VisualStudio.CSharp.Services.Language.CodeModel.CCodeModelBase.ValidateIdentifier(String inputString, Boolean allowDotted, Boolean allowOperatorKeyword)
   at Microsoft.VisualStudio.CSharp.Services.Language.CodeModel.CFileCodeModel.AppendTypeName(ParseTreeNode nodeCtx, Object pVT, String& sbstr)
   at Microsoft.VisualStudio.CSharp.Services.Language.CodeModel.CFileCodeModel.Format(CInsertInfo pInfo, CCodeElementBase pContext, CStringBuilder sb, String pszFmt, Object[] args)
   at Microsoft.VisualStudio.CSharp.Services.Language.CodeModel.CCodeTypeBase.AddMember[T](String pszName, Object vtPosition, MEMBERKIND iKind, Name pKey, Object[] args)
   at Microsoft.VisualStudio.CSharp.Services.Language.CodeModel.CCodeClass.AddFunction(String bstrName, vsCMFunction FuncKind, Object vtType, Object vtPosition, vsCMAccess Access, Object vtLocation)
   at EnvDTE80.CodeClass2.AddFunction(String Name, vsCMFunction Kind, Object Type, Object Position, vsCMAccess Access, Object Location)
   at XXXXXXXX.SQLServer.SQLCodeGenerator.AddFinderMethod() in C:\Source\XXXXXXXX.XXXXXXXX\XXXXXXXX.Application\XXXXXXXX.SQLServer\SQLCodeGenerator.cs:line 254
   at XXXXXXXX.SQLServer.SQLCodeGenerator.generateEntityServiceClass2() in C:\Source\XXXXXXXX.XXXXXXXX\XXXXXXXX.Application\XXXXXXXX.SQLServer\SQLCodeGenerator.cs:line 221

     private CodeClass2 CreateClass(EntityClassType entityClassType)
        {
            string fileName = string.Empty;
            string className = string.Empty;

            switch (entityClassType)
            {
                case EntityClassType.Entity:
                    fileName = entityFileName;
                    className = tableNameAsSingular;
                    break;
                case EntityClassType.EntityService:
                    fileName = entityServiceFileName;
                    className = tableNameAsSingular + "Service";
                    break;
            }

            ProjectItems projectItems;
            ProjectItem projectItem;
            String csItemTemplatePath;
            Solution2 soln;
            soln = (Solution2)EnvDte.Solution;
            projectItems = EnvDte.ActiveDocument.ProjectItem.ProjectItems;
            csItemTemplatePath = soln.GetProjectItemTemplate("CodeFile", "CSharp");
            projectItems.AddFromTemplate(csItemTemplatePath, fileName);
            projectItem = soln.FindProjectItem(fileName);
            FileCodeModel2 fcm = (FileCodeModel2)projectItem.FileCodeModel;

            switch (entityClassType)
            {
                case EntityClassType.Entity:
                    break;
                case EntityClassType.EntityService:
                    fcm.AddImport("System.Linq");
                    fcm.AddImport("System.Collections.Generic");
                    break;
            }

            EnvDTE.CodeNamespace projectCodeNamespace;

            try
            {
                // Add a namespace.
                projectCodeNamespace = fcm.AddNamespace(projectNamespace, -1);

                if (projectCodeNamespace != null)
                {
                    // Add a class to the namespace.
                    return (CodeClass2)projectCodeNamespace.AddClass(className, -1, null, null, vsCMAccess.vsCMAccessPublic);
                }
            }
            catch (Exception ex)
            {
                // throw ex;
            }
            return null;
        }

entityServiceClass is a CodeClass2 object , tableNameAsSingular is the name of the class and it is public too.

Do I need to save the Entity Class before I can do this?

BDC Meta Man Web Edition - Out Now
Phill Duffy [MCTS]  Tuesday, October 13, 2009 11:41 AM
I should add if I do:

 public void AddFinderMethod()
        {
            string t = 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.
        }





it does work
BDC Meta Man Web Edition - Out Now
Phill Duffy [MCTS]  Tuesday, October 13, 2009 11:52 AM
Hello, Phill
I have tried my best, but I am still not able to reproduce this error. I use almost the same code with you, but IEnumerable<ProductEntity> is a right identifier on my side. Really sorry for that.
Thanks
Chao
Chao Kuo  Wednesday, October 14, 2009 5:06 AM
D'oh. I was hoping for some wonderful news :) I really appreciate you trying to replicate and solve my issue. I really need this fixed so I will post here if\when I manage it.

Best Regards,

Phill
BDC Meta Man Web Edition - Out Now
Phill Duffy [MCTS]  Wednesday, October 14, 2009 8:11 AM
Please could you send me your solution to compare?
BDC Meta Man Web Edition - Out Now
Phill Duffy [MCTS]  Wednesday, October 14, 2009 10:43 AM
I have managed to get it working with the following:

string returnType = "System.Collections.Generic.IEnumerable<" + tableNameAsSingular + ">"; <br />
CodeFunction2 finderFunction = (CodeFunction2)entityServiceClass.AddFunction("Get" + table.Name, vsCMFunction.vsCMFunctionFunction, returnType, -1, vsCMAccess.vsCMAccessPublic, null);
           
Why Chao's worked without the full name is of wonder but heck, I am a happy bunny!!!!!!!!!!!!!

BDC Meta Man Web Edition - Out Now
Phill Duffy [MCTS]  Wednesday, October 14, 2009 1:50 PM
Hi, Phill
I am sorry I am falling asleep that time yesterday because that time was night in far east asian.
Thank goodness, you got it worked. And You are so kind to share your answer with others.
I spent a whole morning to reproduce your errors, but I did not work out. So I have to give up. It is so strange that I work it out without full work.
Thanks
Chao
Chao Kuo  Thursday, October 15, 2009 2:26 AM

You can use google to search for other answers

Custom Search

More Threads

• Hierutil7 and VSEnvSdkUtil projects not available with the Visual Studio 2008 SDK.
• Refresh After Get Latest
• VSPackage with core editor and contained language
• Webtest and Visual Studio 2008.
• Using manifests for Add-ins (UAC)
• Problem while Renaming an item
• Decorator for Compartments
• Changes in new DSL project not reflected while debugging
• my-language-IDE
• Visual Web Developer Extension Architecture