{"id":4419,"date":"2014-03-30T10:49:25","date_gmt":"2014-03-30T10:49:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/using-reflection-emit-to-copy-a-custom-attribute-to-another-method-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:49:25","modified_gmt":"2014-03-30T10:49:25","slug":"using-reflection-emit-to-copy-a-custom-attribute-to-another-method-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/using-reflection-emit-to-copy-a-custom-attribute-to-another-method-collection-of-common-programming-errors\/","title":{"rendered":"Using Reflection.Emit to copy a custom attribute to another method-Collection of common programming errors"},"content":{"rendered":"<p>The answer you (frjames) posted is close, but doesn&#8217;t account for property initializers like&#8230;<\/p>\n<pre><code>[ServiceBehavior(Name=\"ServiceName\")]\n<\/code><\/pre>\n<p>However, the idea of converting CustomAttributeData to a CustomAttributeBuilder for use in Reflection.Emit is right on.<\/p>\n<p>I ended up having to do this for an open source project (Autofac) and came up with this extension method:<\/p>\n<pre><code>public static CustomAttributeBuilder ToAttributeBuilder(this CustomAttributeData data)\n{\n  if (data == null)\n  {\n    throw new ArgumentNullException(\"data\");\n  }\n\n  var constructorArguments = new List();\n  foreach (var ctorArg in data.ConstructorArguments)\n  {\n    constructorArguments.Add(ctorArg.Value);\n  }\n\n  var propertyArguments = new List();\n  var propertyArgumentValues = new List();\n  var fieldArguments = new List();\n  var fieldArgumentValues = new List();\n  foreach (var namedArg in data.NamedArguments)\n  {\n    var fi = namedArg.MemberInfo as FieldInfo;\n    var pi = namedArg.MemberInfo as PropertyInfo;\n\n    if (fi != null)\n    {\n      fieldArguments.Add(fi);\n      fieldArgumentValues.Add(namedArg.TypedValue.Value);\n    }\n    else if (pi != null)\n    {\n      propertyArguments.Add(pi);\n      propertyArgumentValues.Add(namedArg.TypedValue.Value);\n    }\n  }\n  return new CustomAttributeBuilder(\n    data.Constructor,\n    constructorArguments.ToArray(),\n    propertyArguments.ToArray(),\n    propertyArgumentValues.ToArray(),\n    fieldArguments.ToArray(),\n    fieldArgumentValues.ToArray());\n}\n<\/code><\/pre>\n<p>That one accounts for all the ways to initialize the attribute.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The answer you (frjames) posted is close, but doesn&#8217;t account for property initializers like&#8230; [ServiceBehavior(Name=&#8221;ServiceName&#8221;)] However, the idea of converting CustomAttributeData to a CustomAttributeBuilder for use in Reflection.Emit is right on. I ended up having to do this for an open source project (Autofac) and came up with this extension method: public static CustomAttributeBuilder ToAttributeBuilder(this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4419","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4419"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4419\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}