<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. 如何在运行时将属性添加到属性

      时间:2023-07-27
    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");
                }
                

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

                上一篇:从描述属性中获取枚举 下一篇:从枚举属性中获取枚举

                相关文章

                <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>
                2. <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'>