• <tfoot id='p7L6l'></tfoot>
        <bdo id='p7L6l'></bdo><ul id='p7L6l'></ul>

      <small id='p7L6l'></small><noframes id='p7L6l'>

        <legend id='p7L6l'><style id='p7L6l'><dir id='p7L6l'><q id='p7L6l'></q></dir></style></legend>

      1. <i id='p7L6l'><tr id='p7L6l'><dt id='p7L6l'><q id='p7L6l'><span id='p7L6l'><b id='p7L6l'><form id='p7L6l'><ins id='p7L6l'></ins><ul id='p7L6l'></ul><sub id='p7L6l'></sub></form><legend id='p7L6l'></legend><bdo id='p7L6l'><pre id='p7L6l'><center id='p7L6l'></center></pre></bdo></b><th id='p7L6l'></th></span></q></dt></tr></i><div id='p7L6l'><tfoot id='p7L6l'></tfoot><dl id='p7L6l'><fieldset id='p7L6l'></fieldset></dl></div>
      2. 我可以将属性与匿名类一起使用吗?

        Can I use Attributes with Anonymous classes?(我可以将属性与匿名类一起使用吗?)
      3. <legend id='20S2M'><style id='20S2M'><dir id='20S2M'><q id='20S2M'></q></dir></style></legend>
        <i id='20S2M'><tr id='20S2M'><dt id='20S2M'><q id='20S2M'><span id='20S2M'><b id='20S2M'><form id='20S2M'><ins id='20S2M'></ins><ul id='20S2M'></ul><sub id='20S2M'></sub></form><legend id='20S2M'></legend><bdo id='20S2M'><pre id='20S2M'><center id='20S2M'></center></pre></bdo></b><th id='20S2M'></th></span></q></dt></tr></i><div id='20S2M'><tfoot id='20S2M'></tfoot><dl id='20S2M'><fieldset id='20S2M'></fieldset></dl></div>
            <tfoot id='20S2M'></tfoot>
              <tbody id='20S2M'></tbody>

              <small id='20S2M'></small><noframes id='20S2M'>

                • <bdo id='20S2M'></bdo><ul id='20S2M'></ul>
                  本文介绍了我可以将属性与匿名类一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个匿名类:

                  var someAnonymousClass = new
                  {
                      SomeInt = 25,
                      SomeString = "Hello anonymous Classes!",
                      SomeDate = DateTime.Now
                  };
                  

                  是否有附加属性到这个类?反射,其他?我真的很希望有这样的东西:

                  Is there anyway to attach Attributes to this class? Reflection, other? I was really hoping for something like this:

                  var someAnonymousClass = new
                  {
                      [MyAttribute()]
                      SomeInt = 25,
                      SomeString = "Hello anonymous Classes!",
                      SomeDate = DateTime.Now
                  };
                  

                  推荐答案

                  您实际上是在创建所谓的匿名类型,而不是动态类型.

                  You're actually creating what is called an anonymous type here, not a dynamic one.

                  不幸的是,没有办法实现您想要做的事情.匿名类型是一种非常简单的不可变类型,由名称/值对组成.

                  Unfortunately no there is no way to achieve what you are trying to do. Anonymous types are meant to be a very simple immutable type consisting of name / value pairs.

                  匿名类型的 C# 版本仅允许您自定义基础类型上的名称/值对集.没有其他的.VB.Net 允许更多的自定义,因为这些对可以是可变的或不可变的.但是,它们都不允许您使用属性来增加类型.

                  The C# version of anonymous type only allows you to customize the set of name / value pairs on the underlying type. Nothing else. VB.Net allows slightly more customization in that the pairs can be mutable or immutable. Neither allow you to augment the type with attributes though.

                  如果你想添加属性,你需要创建一个完整的类型.

                  If you want to add attributes you'll need to create a full type.

                  EDIT OP 询问是否可以通过反射添加属性.

                  EDIT OP asked if the attributes could be added via reflection.

                  不,这是不可能的.反射是一种检查元数据而不改变它的方法.因此它不能用于添加属性.

                  No this cannot be done. Reflection is a way of inspecting metadata not mutating it. Hence it cannot be used to add attributes.

                  此外,程序集中的类型定义,一般来说,是不可变的,不能在运行时改变 [1].这包括向方法添加属性.所以这里也不能使用其他类似反射的技术.

                  Additionally, type definitions in an assembly, and in general, are immutable and cannot be mutated at runtime [1]. This includes the adding of attributes to a method. So other reflection like technologies cannot be used here either.

                  [1] ENC 操作是一个例外

                  [1] The one exception to this is ENC operation

                  这篇关于我可以将属性与匿名类一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)

                    • <bdo id='VW3Gh'></bdo><ul id='VW3Gh'></ul>

                        <tbody id='VW3Gh'></tbody>

                      <small id='VW3Gh'></small><noframes id='VW3Gh'>

                      <legend id='VW3Gh'><style id='VW3Gh'><dir id='VW3Gh'><q id='VW3Gh'></q></dir></style></legend>

                        1. <tfoot id='VW3Gh'></tfoot>
                          <i id='VW3Gh'><tr id='VW3Gh'><dt id='VW3Gh'><q id='VW3Gh'><span id='VW3Gh'><b id='VW3Gh'><form id='VW3Gh'><ins id='VW3Gh'></ins><ul id='VW3Gh'></ul><sub id='VW3Gh'></sub></form><legend id='VW3Gh'></legend><bdo id='VW3Gh'><pre id='VW3Gh'><center id='VW3Gh'></center></pre></bdo></b><th id='VW3Gh'></th></span></q></dt></tr></i><div id='VW3Gh'><tfoot id='VW3Gh'></tfoot><dl id='VW3Gh'><fieldset id='VW3Gh'></fieldset></dl></div>