Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取未知大小(C#)的结构中的所有变量_C#_Combobox_Struct - Fatal编程技术网

获取未知大小(C#)的结构中的所有变量

获取未知大小(C#)的结构中的所有变量,c#,combobox,struct,C#,Combobox,Struct,我有一个项目,在这个项目中,我必须从另一个类中的结构中获取所有变量,并将它们的所有名称添加到表单中的组合框中。目前我的主要问题是以一种可以将每个变量单独添加到组合框的方式迭代结构。我试着这样做: msgVars vars = new msgVars(); foreach (object obj in vars) { comboBox1.Items.Add(GetName(obj)); } 但正如您可能知道的

我有一个项目,在这个项目中,我必须从另一个类中的结构中获取所有变量,并将它们的所有名称添加到表单中的组合框中。目前我的主要问题是以一种可以将每个变量单独添加到组合框的方式迭代结构。我试着这样做:

        msgVars vars = new msgVars();
        foreach (object obj in vars)
        {
            comboBox1.Items.Add(GetName(obj));
        }
但正如您可能知道的,您不能以这种方式迭代结构。有没有办法不用硬编码变量就可以做到这一点

另外,作为参考,这是GetName函数:

static string GetName<T>(T item) where T : struct
{
    var properties = typeof(T).GetProperties();
    if (properties.Length == 1)
    {
        return properties[0].Name;
    }
    else
    {
        return null;
    }
}
静态字符串GetName(T项),其中T:struct
{
var properties=typeof(T).GetProperties();
如果(properties.Length==1)
{
返回属性[0]。名称;
}
其他的
{
返回null;
}
}

如果您正在寻找一种在运行时访问结构(或类)属性的方法,而无需在代码中预定义该属性集,那么您可能需要使用反射

下面是一个例子:

struct MyStruct
{
    private readonly int m_Age;
    private readonly string m_LastName;
    private readonly string m_FirstName;

    public int Age { get { return m_Age; } }
    public string LastName { get { return m_LastName; } }
    public string FirstName { get { return m_FirstName; } }

    public MyStruct( int age, string last, string first )
    {
        m_Age = age;
        m_LastName = last;
        m_FirstName = first;
    }
}

class StructReflection
{
    public static void Main(string[] args)
    {
        var theStruct = new MyStruct( 40, "Smith", "John" );
        PrintStructProperties( theStruct );
    }

    public static void PrintStructProperties( MyStruct s )
    {
        // NOTE: This code will not handle indexer properties...
        var publicProperties = s.GetType().GetProperties();
        foreach (var prop in publicProperties)
            Console.WriteLine( "{0} = {1}", prop.Name, prop.GetValue( s, null ) );
    }
}

您可以尝试使用反射。将信息存储在哈希表中如何

public Hashtable GetPropertyInfo(Person person)
{
    var properties = new Hashtable();
    PropertyInfo[] propInfo = person.GetType().GetProperties();
    foreach (PropertyInfo prop in propInfo)
    {
         properties.Add(prop.Name, prop.GetValue(person, null));
    }

    return properties;
}
然后,您可以通过以下方式写出信息:

var person = new Person()
Person.Name = "Test";
Person.Age = 21;

var PropertyInfo = GetPropertyInfo(person);
foreach (string key in PropertyInfo.Keys)
{
     Console.WriteLine("{0} = {1}", key, PropertyInfo[key]);    
}

结构是单个实体,而不是变量的集合。这意味着您不能对其属性进行“迭代”。您需要做的是获取属性名的集合并对其进行迭代。GetName函数无法执行此操作,因为它只返回第一个属性的名称

要将属性名称添加到组合,只需执行以下操作:

 var vars = new msgVars();
 foreach(var name in GetNames(vars))
     comboBox1.Items.Add(name);
事实上,获取属性名非常简单,您可以完全摆脱GetNames,只需编写

  foreach (var prop in typeof(msgVars).GetProperties())
      comboBox1.Items.Add(prop.Name);
有多种方法可以编写GetNames来返回名称集合。您可以用属性名填充列表,尽管最简单的方法是让它返回一个迭代器,如下所示:

 public static IEnumerable<string> GetNames<T>(T obj) where T:struct
 {
     var properties = typeof (T).GetProperties();
     foreach (var propertyInfo in properties)
     {
         yield return propertyInfo.Name;
     }
 }
公共静态IEnumerable GetNames(T obj),其中T:struct
{
var properties=typeof(T).GetProperties();
foreach(属性中的var propertyInfo)
{
收益返回属性info.Name;
}
}
最后,您实际上不需要将结构的实例传递给方法,因为您正在枚举结构的属性名,而不是它们的值。您可以像这样重写GetNames

 public static IEnumerable<string> GetNames<T>() where T:struct
 {
     var properties = typeof (T).GetProperties();
     foreach (var propertyInfo in properties)
     {
        yield return propertyInfo.Name;
     }
  }
  foreach(var name in GetNames<msgVars>())
      comboBox1.Items.Add(name);
public静态IEnumerable GetNames(),其中T:struct
{
var properties=typeof(T).GetProperties();
foreach(属性中的var propertyInfo)
{
收益返回属性info.Name;
}
}
然后像这样加载名称

 public static IEnumerable<string> GetNames<T>() where T:struct
 {
     var properties = typeof (T).GetProperties();
     foreach (var propertyInfo in properties)
     {
        yield return propertyInfo.Name;
     }
  }
  foreach(var name in GetNames<msgVars>())
      comboBox1.Items.Add(name);
foreach(GetNames()中的变量名)
comboBox1.Items.Add(名称);

遍历变量类型的属性如何?定义“结构中的变量”。你指的是“属性”,对吗?请编辑您的问题以解决此问题。