C# 动态创建 Type 数组

发布于 2024-10-11 19:37:30 字数 1040 浏览 20 评论 0原文

在 C# 中,我需要能够在运行时基于以字符串形式传递给函数的逗号分隔数据类型列表创建 Type 对象数组。基本上,这就是我想要完成的任务:

// create array of types
Type[] paramTypes = { typeof(uint), typeof(string), typeof(string), typeof(uint) };

但是我需要能够像这样调用我的函数:

MyFunction("uint, string, string, uint");

并让它根据传入的字符串动态生成数组。这是我的第一次尝试:

void MyFunction(string dataTypes)
{
    //out or in parameters of your function.   
    char[] charSeparators = new char[] {',', ' '};
    string[] types = dataTypes.Split(charSeparators,
                        stringSplitOptions.RemoveEmptyEntries);

    // create a list of data types for each argument
    List<Type> listTypes = new List<Type>();
    foreach (string t in types)
    {
        listTypes.Add(Type.GetType(t)); 
    }
    // convert the list to an array
    Type [] paramTypes = listTypes.ToArray<Type>();

}

此代码只是创建一个数组 System.Type 类型的空对象。我很确定问题出在这里:

listTypes.Add(Type.GetType(t));

关于为什么这种语法不起作用的建议?

In C#, I need to be able to create an array of Type objects at run-time based on a comma-delimited list of data types passed in to a function as a string. Basically, here is what I am trying to accomplish:

// create array of types
Type[] paramTypes = { typeof(uint), typeof(string), typeof(string), typeof(uint) };

But I need to be able to call my function like this:

MyFunction("uint, string, string, uint");

and have it generate the array dynamically based on the string passed in. Here was my first attempt:

void MyFunction(string dataTypes)
{
    //out or in parameters of your function.   
    char[] charSeparators = new char[] {',', ' '};
    string[] types = dataTypes.Split(charSeparators,
                        stringSplitOptions.RemoveEmptyEntries);

    // create a list of data types for each argument
    List<Type> listTypes = new List<Type>();
    foreach (string t in types)
    {
        listTypes.Add(Type.GetType(t)); 
    }
    // convert the list to an array
    Type [] paramTypes = listTypes.ToArray<Type>();

}

This code simply creates an array of null objects of type System.Type. I'm pretty sure the problem lies here:

listTypes.Add(Type.GetType(t));

Suggestions on why this syntax does not do the trick?

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

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

发布评论

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

评论(5

羞稚 2024-10-18 19:37:30

问题是 .NET 中没有 uintstring 类型。这些是实际 System.UInt32 的 C# 类型别名和 < a href="http://msdn.microsoft.com/en-us/library/system.string.aspx" rel="noreferrer">System.String 类型。所以你应该这样调用你的函数:

MyFunction("System.UInt32, System.String, System.String, System.UInt32");

The problem is that there are no uint and string types in .NET. Those are C# type aliases to the actual System.UInt32 and System.String types. So you should call your function like this:

MyFunction("System.UInt32, System.String, System.String, System.UInt32");
寻梦旅人 2024-10-18 19:37:30

传入 System.StringSystem.Int32 而不是 stringint

“string”只是 System.String 的简写。 Type.GetType 将不接受类型的简写符号。

Pass in System.String, System.Int32 instead of string and int.

"string" is just shorthand for System.String. Type.GetType will not accept shorthand notation for types.

春夜浅 2024-10-18 19:37:30

使用每种类型的全名,包括命名空间。就像这样:

class Program
{
    static void Main(string[] args)
    {
        var dataTypes = "System.UInt32, System.String, System.String, System.UInt32";

        //out or in parameters of your function.   
        char[] charSeparators = new char[] { ',', ' ' };
        string[] types = dataTypes.Split(charSeparators,
                            StringSplitOptions.RemoveEmptyEntries);

        // create a list of data types for each argument
        List<Type> listTypes = new List<Type>();
        foreach (string t in types)
        {
            listTypes.Add(Type.GetType(t));
        }
        // convert the list to an array
        Type[] paramTypes = listTypes.ToArray<Type>();
    }
}

Use the full name for each type, including namespace. Like so:

class Program
{
    static void Main(string[] args)
    {
        var dataTypes = "System.UInt32, System.String, System.String, System.UInt32";

        //out or in parameters of your function.   
        char[] charSeparators = new char[] { ',', ' ' };
        string[] types = dataTypes.Split(charSeparators,
                            StringSplitOptions.RemoveEmptyEntries);

        // create a list of data types for each argument
        List<Type> listTypes = new List<Type>();
        foreach (string t in types)
        {
            listTypes.Add(Type.GetType(t));
        }
        // convert the list to an array
        Type[] paramTypes = listTypes.ToArray<Type>();
    }
}
ㄖ落Θ余辉 2024-10-18 19:37:30

它不起作用,因为 uintstring 等不是 .net 类型的正式名称。它们是 System.UInt32System.String 等的 C# 别名。如果您想像这样动态创建类型,则需要使用 .net 类型名称。

It doesn't work because uint, string, etc are not the official names of the .net types. They're C# aliases for System.UInt32, System.String, etc. You'll need to use the .net type names if you want to create types dynamically like that.

旧瑾黎汐 2024-10-18 19:37:30

我知道这很旧,但这就是我破解它的方法:

private void Build_Files_DT()
    {
        String[] CLM_Header_Name_Array; Object[] Data_Type_Array;
        CLM_Header_Name_Array = new String[] { "File_Name_CLM", "Created_Date_Time_CLM", "Log_Read_CLM" };
        Data_Type_Array = new Object[] { typeof(System.String), typeof(System.DateTime), typeof(System.Boolean) };
        DT = new DataTable();
        Build_DT(DT, CLM_Header_Name_Array, Data_Type_Array);
    }

private void Build_DT(DataTable DT,  String[] CLM_Header_Name_Array, Object[] Data_Type_Array)
    {

        for (int i = 0; i < CLM_Header_Name_Array.Length; i++)
        {
            DT.Columns.Add(new DataColumn(CLM_Header_Name_Array[i], (Type)Data_Type_Array[i]));
        }
    }//Build_DT

I know this is old but this is how i cracked it:

private void Build_Files_DT()
    {
        String[] CLM_Header_Name_Array; Object[] Data_Type_Array;
        CLM_Header_Name_Array = new String[] { "File_Name_CLM", "Created_Date_Time_CLM", "Log_Read_CLM" };
        Data_Type_Array = new Object[] { typeof(System.String), typeof(System.DateTime), typeof(System.Boolean) };
        DT = new DataTable();
        Build_DT(DT, CLM_Header_Name_Array, Data_Type_Array);
    }

private void Build_DT(DataTable DT,  String[] CLM_Header_Name_Array, Object[] Data_Type_Array)
    {

        for (int i = 0; i < CLM_Header_Name_Array.Length; i++)
        {
            DT.Columns.Add(new DataColumn(CLM_Header_Name_Array[i], (Type)Data_Type_Array[i]));
        }
    }//Build_DT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文