如何使用 IEnumerable<> 创建 CodeFunction2类型?

发布于 2024-08-08 08:37:00 字数 1198 浏览 2 评论 0 原文

我真的需要创建类似下面的东西,我正在构建2个类,第一个是一个名称为 tableNameAsSingular (即 AddressEntity) 的类,在我的第二个工作类中,我需要有类似下面的东西

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

创建函数时我有以下..

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

但 t 始终为 null

当我执行 Type.GetType(tableNameAsSingular) 时,它也返回 null

任何帮助或指针都会受到热烈欢迎。另外,如果有人知道大量的 EnvDTE 代码生成知识在哪里,我将非常感激!


更新

我现在已经使用以下内容尝试将其作为字符串使用:

   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.
    }

但我在 AddFunction 方法中收到“IEnumerable 不是有效标识符”错误消息

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2024-08-15 08:37:00

语法 IEnumerable 是 C# 语法,而不是 .NET 语法(使用反引号、计数器等)。您的意思是:

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

请注意,Assembly.GetType 通常是更好的选择,因为您可以只使用命名空间限定的名称:

Assembly asm = typeof(SomeKnownType).Assembly;
Type tableType = asm.GetType(namespaceQualifiedNameToEntity);

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);
递刀给你 2024-08-15 08:37:00

已成功使其与以下内容一起工作:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文