locked
How to create a function with the complex data type that the function returns using CodeInterface2.AddFunction RRS feed

  • Question

  • Hi all,

    I need to add a function to my C# interface and I'm using this method do do that. My function has to return the complex data type

    List<Departments> GetDepartments();
    

    What should I pass to Type parameter of AddFunction method? In the description of AddFunction method we have:
    Type

    Required. A vsCMTypeRef constant indicating the data type that the function returns. This can be a CodeTypeRef object, a vsCMTypeRef constant, or a fully qualified type name.

    What is the proper way to add a complex data type?


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Wednesday, January 11, 2012 5:43 PM

Answers

  • I don't have any kind of repro/package that works with the code model stuff written and I don't have time to write one at the moment. 

    I glanced at the C# code and it appears that if you pass in a string for the type param it will simply try to paste that in to the code spit, however I would pass in the fully qualified name and not rely on there be say a using statement active in the code file.  So pass in "System.Collections.Generic.List<Departments>" as your type name. 

    It looks like, if the C# compiler can determine that truncating the name (to just List<blah>) is possible (i.e. there is an appropriate using statement or alias in the target file) then it will, otherwise it will leave it fully qualified.

    Ryan

    Thursday, January 12, 2012 7:46 PM
  • CodeModel is a COM based interface, there are limited ways you could pass them, you certainly can't pass managed types as COM has no ideas about managed type objects.  I imagine a better way would be something like typeof(<.NET type here>).FullName or something similar, so you aren't hardcoding string literals everywhere, but you would have to see what that returns and if it is in a format that the codemodel stuff understands.

    As for best practice, I don't know, my team doesn't own CodeModel stuff and I have basically no familiarity with it.  It seems supported (i.e. the code appears to accept strings), not sure what else you would use for an arbitrary type with complexities like generic types.

    Ryan

    Thursday, January 12, 2012 10:21 PM

All replies

  • What language is this?  C# I assume?  Have you tried the fully qualified type name as they suggest?

    Ryan

    Wednesday, January 11, 2012 8:51 PM
  • Hi Ryan,

    Yes, it's C#. Please give an example how to use AddFunction with the fully qualified type name in order to add this method

    List<Departments> GetDepartments();

    to the interface.

    I have to modify this my code

    namespace MyCaller
    {
        public interface IGetData
        {
        }
    }
    

    As a result I wish to have

    namespace MyCaller
    {
        public interface IGetData
        {
            List<Departments> GetDepartments();
        }
    }
    


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Thursday, January 12, 2012 8:20 AM
  • I don't have any kind of repro/package that works with the code model stuff written and I don't have time to write one at the moment. 

    I glanced at the C# code and it appears that if you pass in a string for the type param it will simply try to paste that in to the code spit, however I would pass in the fully qualified name and not rely on there be say a using statement active in the code file.  So pass in "System.Collections.Generic.List<Departments>" as your type name. 

    It looks like, if the C# compiler can determine that truncating the name (to just List<blah>) is possible (i.e. there is an appropriate using statement or alias in the target file) then it will, otherwise it will leave it fully qualified.

    Ryan

    Thursday, January 12, 2012 7:46 PM
  • Hi Ryan,

    So the code should look like this, correct?

    CodeInterface2 codeInterface = GetCodeInterfaceElementSomehow();
    CodeFunction codeFunction = codeInterface.AddFunction("GetDepartments", vsCMFunction.vsCMFunctionFunction, "System.Collections.Generic.List<Departments>", -1);
    

     

    Is it the best practice to pass the complex data type just as a string?


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Thursday, January 12, 2012 9:17 PM
  • CodeModel is a COM based interface, there are limited ways you could pass them, you certainly can't pass managed types as COM has no ideas about managed type objects.  I imagine a better way would be something like typeof(<.NET type here>).FullName or something similar, so you aren't hardcoding string literals everywhere, but you would have to see what that returns and if it is in a format that the codemodel stuff understands.

    As for best practice, I don't know, my team doesn't own CodeModel stuff and I have basically no familiarity with it.  It seems supported (i.e. the code appears to accept strings), not sure what else you would use for an arbitrary type with complexities like generic types.

    Ryan

    Thursday, January 12, 2012 10:21 PM
  • Thanks Ryan for your help!

    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Friday, January 13, 2012 9:30 AM