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

    <bdo id='eXJuH'></bdo><ul id='eXJuH'></ul>
<tfoot id='eXJuH'></tfoot>

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

    1. <small id='eXJuH'></small><noframes id='eXJuH'>

    2. 如何在运行时将属性添加到属性

      How to add an attribute to a property at runtime(如何在运行时将属性添加到属性)
    3. <i id='6R6oP'><tr id='6R6oP'><dt id='6R6oP'><q id='6R6oP'><span id='6R6oP'><b id='6R6oP'><form id='6R6oP'><ins id='6R6oP'></ins><ul id='6R6oP'></ul><sub id='6R6oP'></sub></form><legend id='6R6oP'></legend><bdo id='6R6oP'><pre id='6R6oP'><center id='6R6oP'></center></pre></bdo></b><th id='6R6oP'></th></span></q></dt></tr></i><div id='6R6oP'><tfoot id='6R6oP'></tfoot><dl id='6R6oP'><fieldset id='6R6oP'></fieldset></dl></div>
      • <legend id='6R6oP'><style id='6R6oP'><dir id='6R6oP'><q id='6R6oP'></q></dir></style></legend>
      • <small id='6R6oP'></small><noframes id='6R6oP'>

            <bdo id='6R6oP'></bdo><ul id='6R6oP'></ul>

                <tbody id='6R6oP'></tbody>
              <tfoot id='6R6oP'></tfoot>

                本文介绍了如何在运行时将属性添加到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                //Get PropertyDescriptor object for the given property name
                var propDesc = TypeDescriptor.GetProperties(typeof(T))[propName];
                
                //Get FillAttributes methodinfo delegate
                var methodInfo = propDesc.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public |
                                                                      BindingFlags.NonPublic)
                    .FirstOrDefault(m => m.IsFamily || m.IsPublic && m.Name == "FillAttributes");
                
                //Create Validation attribute
                var attribute = new RequiredAttribute();
                var  attributes= new ValidationAttribute[]{attribute};
                
                //Invoke FillAttribute method
                methodInfo.Invoke(propDesc, new object[] { attributes });
                

                您好,我正在尝试使用上述代码在运行时添加 Validation 属性.但是我得到以下异常:

                Hi I am trying to add Validation attribute at runtime using the above code. However I am getting the below exception:

                集合的大小是固定的

                推荐答案

                不要让别人告诉你你做不到.如果你愿意,你可以竞选总统:-)

                Don't let someone tell you that you can't do it. You can run for president if you want :-)

                为方便起见,这是一个完整的示例

                For your convenience, this is a fully working example

                public class SomeAttribute : Attribute
                {
                    public SomeAttribute(string value)
                    {
                        this.Value = value;
                    }
                
                    public string Value { get; set; }
                }
                
                public class SomeClass
                {
                    public string Value = "Test";
                }
                
                [TestMethod]
                public void CanAddAttribute()
                {
                    var type = typeof(SomeClass);
                
                    var aName = new System.Reflection.AssemblyName("SomeNamespace");
                    var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
                    var mb = ab.DefineDynamicModule(aName.Name);
                    var tb = mb.DefineType(type.Name + "Proxy", System.Reflection.TypeAttributes.Public, type);
                
                    var attrCtorParams = new Type[] { typeof(string) };
                    var attrCtorInfo = typeof(SomeAttribute).GetConstructor(attrCtorParams);
                    var attrBuilder = new CustomAttributeBuilder(attrCtorInfo, new object[] { "Some Value" });
                    tb.SetCustomAttribute(attrBuilder);
                
                    var newType = tb.CreateType();
                    var instance = (SomeClass)Activator.CreateInstance(newType);
                
                    Assert.AreEqual("Test", instance.Value);
                    var attr = (SomeAttribute)instance.GetType()
                        .GetCustomAttributes(typeof(SomeAttribute), false)
                        .SingleOrDefault();
                    Assert.IsNotNull(attr);
                    Assert.AreEqual(attr.Value, "Some Value");
                }
                

                这篇关于如何在运行时将属性添加到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 中的代码生成属性相关联)

                    <tbody id='nh0wk'></tbody>

                  <legend id='nh0wk'><style id='nh0wk'><dir id='nh0wk'><q id='nh0wk'></q></dir></style></legend>
                    <bdo id='nh0wk'></bdo><ul id='nh0wk'></ul>

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

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