简体   繁体   English

如何在C#中使用变量的值作为键来声明匿名类型?

[英]how to declare anonymous type using a variable's value as key in c#?

Is there anyway to succeed this sample? 无论如何,有没有成功的例子?

string[] columns = ["a","b","c","d"];
var headers = from column in columns
select new
{
  title = column,
  filter = new  { column = "select" },
  show = true
}; 

After debug this code block, I see header.filter.column property. 调试此代码块后,我看到header.filter.column属性。 How can I see header.filter.a ? 我如何看到header.filter.a

This is not possible, since anynonymous types are anonymous , but they still exist at compile time . 这是不可能的,因为任何匿名类型都是匿名的 ,但是它们在编译时仍然存在。 So you can not create an anonymous class with runtime member names. 因此,您不能使用运行时成员名称创建匿名类。 What you are actually doing is to set a property named column with the value "select" . 您实际上正在做的是设置一个名为column的属性,其值为"select"

What you can do though is to create a dictionary from your result and access the data with the dictionary: 但是,您可以做的是根据结果创建字典并使用字典访问数据:

var headerFilterDictionary = headers.ToDictionary(item => item.title, item => item.filter.column);

var columnFilterValue = headerFilterDictionary["a"];

This probably isn't what you really want to do (at all), but you can make the syntax (mostly) work by using dynamic : 这可能根本不是您真正想做的,但是您可以使用dynamic使语法(主要是)起作用:

    static void Main(string[] args)
    {
        string[] columns = { "a", "b", "c", "d" };
        var headers = from column in columns

                      let c_a = (dynamic)(column == "a" ? new { a = column } : null)
                      let c_b = (dynamic)(column == "b" ? new { b = column } : null)
                      let c_c = (dynamic)(column == "c" ? new { c = column } : null)
                      let c_d = (dynamic)(column == "d" ? new { d = column } : null)

                      select new
                      {
                          title = column,
                          filter = c_a ?? c_b ?? c_c ?? c_d,
                          show = true
                      };

        var filter_0 = headers.ElementAt(0).filter.a;
        var filter_1 = headers.ElementAt(1).filter.b;
        var filter_2 = headers.ElementAt(2).filter.c;
        var filter_3 = headers.ElementAt(3).filter.d;
    }

Obviously, this doesn't scale very well; 显然,这并不能很好地扩展。 and since it's dynamic, you don't get IntelliSense on filter . 并且由于它是动态的,因此您不会在filter上获得IntelliSense。

This is probably best considered for entertainment purposes only. 最好只考虑将其用于娱乐目的。

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

相关问题 是否可以使用变量/动态字段集在C#中声明匿名类型? - Is it possible to declare an anonymous type in C# with a variable/dynamic set of fields? 如何在C#匿名类型中声明“Key”字段? - How do I declare “Key” fields in C# anonymous types? 如何声明具有匿名类型的字段(C#) - How do I declare a Field with Anonymous Type (C#) 在C#中:如何声明类型为键的通用字典和该类型的IEnumerable &lt;&gt;作为值? - In C#: How to declare a generic Dictionary with a type as key and an IEnumerable<> of that type as value? 在 C# 中将匿名类型转换为键/值数组? - In c# convert anonymous type into key/value array? 如何将C#变量声明为HTML类型变量? - How do you declare a C# variable into an HTML type variable? 使用C#声​​明HiddenField的值和Javascript变量 - Use C# to declare HiddenField's value & Javascript Variable 当变量在 C# 中定义匿名 class 时,如何在块外声明变量 - How can I declare a variable outside of a block when the variable defines an anonymous class in C# 使用 HierarchyId 从 c# 匿名类型获取值 - Get value from c# anonymous Type using HierarchyId C#:是否可以在匿名方法中声明局部变量? - C#: Is it possible to declare a local variable in an anonymous method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM