简体   繁体   English

在 C# 中传递结构定义

[英]Passing a Struct definition in C#

I have an issue with passing a struct definition to a function.我在将结构定义传递给函数时遇到问题。 Not an instance of a struct, but the definition.不是结构的实例,而是定义。

We are open to alternative methods of doing what we want, but for right now, this is what we're trying to do -我们对做我们想做的事情的替代方法持开放态度,但就目前而言,这就是我们正在尝试做的事情-

We are writing a user control.我们正在编写一个用户控件。 This control is to display data in list form with headers, but what "list" it might be asked to display is unknown at design time.该控件以带有标题的列表形式显示数据,但在设计时可能要求它显示什么“列表”是未知的。 Right now we're taking the approach that, in order to keep things lightweight, we're going to pass to a Constructor and/or GetHeaders function a Struct definition.现在我们采用的方法是,为了保持轻量级,我们将向构造函数和/或 GetHeaders 函数传递一个 Struct 定义。 The headers would be pulled from the field names found in a passed structure definition, with data later coming in in both individual objects and lists of objects of that structure type.标题将从在传递的结构定义中找到的字段名称中提取,随后数据进入单个对象和该结构类型的对象列表。

Example.例子。 On the Control side:在控制端:

    private void GetHeaders( dynamic _strc )
        {
        //Type _str_type = ((ObjectHandle) _str).Unwrap().GetType();
        FieldInfo[] fi = _strc.GetFields( BindingFlags.Public | BindingFlags.Instance );

        int _i = 0;

        foreach (FieldInfo _f in fi)
            {
            textBox1.Lines[_i] = _f.Name;
            }

        textBox1.Refresh();
        }

-Please note, I'm just trying to make sure I can parse the structure. - 请注意,我只是想确保我可以解析结构。 We don't know if this will actually work yet since we can't get test to compile because of what is below.我们不知道这是否真的有效,因为我们无法编译测试,因为下面的内容。

The user will have their own struct definition to which the Control will not have direct access.用户将拥有自己的结构定义,控件无法直接访问该定义。

    public struct MineStruct
        {
        String ID;      // So we know who they are
        String Name;    // What we call them to their face
        String Nickname;// What we call them behind their back
        String Address; // We know where they live
        int Mileage;    // How far they've gone
        int Millage;    // How much they'll pay.  Oh, they'll pay...
        }

It will be passed at run-time, we had hoped, in something along these lines:它将在运行时通过,我们曾希望,在以下方面:

    private void button1_Click(object sender, EventArgs e)
        {
        GetHeaders( MineStruct );  //<-Error messaage here
        }

The error message we are getting is "'Form1.MineStruct' is a type. which is not valid in the given context" I've tried changing the GetHeaders function to take "Type", among other things, but nothing helps there.我们收到的错误消息是“'Form1.MineStruct' 是一种类型。这在给定的上下文中无效”我尝试将 GetHeaders 函数更改为采用“类型”等,但没有任何帮助。

So, my questions, in order are...所以,我的问题是……

1) Is this a good way to approach the issue? 1)这是解决问题的好方法吗? We're completely open to doing another way, even passing a whole Class, but we'd like to keep it lightweight, which we believe this would be.我们完全愿意采取另一种方式,甚至传递整个类,但我们希望保持轻量级,我们相信这会是。

2) Is it even possible? 2)这甚至可能吗?

3) Is this actually lightweight? 3) 这真的是轻量级的吗?

4) If so, how do we pass a Struct Definition to a function in C#? 4) 如果是这样,我们如何将结构定义传递给 C# 中的函数?

Maybe use a generic method instead of pass dynamic in parameter, to be sure than the parameter is a struct :也许使用通用方法而不是在参数中传递动态,以确保参数是一个结构体:

private void GetHeaders<T>(T str) where T : struct
{
     //Your code...
}    

What you're calling a "definition" is known as a Type in C#.您所说的“定义”在 C# 中称为Type If you know the name of the type you want information about (as is your case), you can use typeof(MineStruct) to get a Type object (which you mentioned you tried as the parameter of GetHeaders ), from which you can call GetFields to get its fields.如果您知道您想要了解的类型的名称(就像您的情况一样),您可以使用typeof(MineStruct)来获取Type对象(您提到您尝试将其作为GetHeaders的参数),您可以从中调用GetFields获取其字段。 If you had an object that you wanted to get type information about, you'd need to call myObj.GetType() instead.如果您有一个想要获取类型信息的对象,则需要调用myObj.GetType()来代替。

As an aside, the struct's fields are private (the default situation in C#), so you'll need to use BindingFlags.NonPublic as in this answer .顺便说一句,结构的字段是private (C# 中的默认情况),因此您需要使用BindingFlags.NonPublic本答案所示

Thank you all VERY MUCH!非常感谢大家! Putting together the various answers, we got it to work.将各种答案放在一起,我们让它起作用了。

1) Changing the receiving parameter in GetHeaders to "Type" and using "typeof" in the calling function got the compiler on our side. 1) 将 GetHeaders 中的接收参数更改为“Type”并在调用函数中使用“typeof”使编译器站在我们这边。

2) Adding the Binding Flag ".NonPublic" opened up the structure for viewing. 2) 添加绑定标志“.NonPublic”打开结构查看。

3) Using a Generic gave us a better approach to filling out the data once the control was established. 3) 使用 Generic 为我们提供了一种更好的方法来在建立控件后填写数据。

I think I've sent upvotes to everyone, but I'm not as familiar with how StackOverflow operates as I'd like to be, so I hope you've all gotten the credit you deserve!我已经向每个人发送了赞成票,但我并不像我希望的那样熟悉 StackOverflow 的运作方式,所以我希望你们都得到了应有的荣誉! Thanks again!!再次感谢!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM