使用Reflection.Emit将自定义属性复制到另一个方法

时间:2010-03-02 18:00:18

标签: c# wcf interface reflection.emit

我正在尝试基于现有接口生成一组新的wcf接口。 我正在使用Reflection.Emit命名空间来完成此任务。我的问题是如何将旧的自定义属性从一个方法复制到新方法。我见过的每个SetCustomAttributes()示例都需要事先知道属性类型。我需要在运行时发现属性类型。有什么想法吗?

4 个答案:

答案 0 :(得分:5)

你(frjames)发布的答案很接近,但没有考虑像...这样的属性初始化程序。

[ServiceBehavior(Name="ServiceName")]

但是,将CustomAttributeData转换为CustomAttributeBuilder以在Reflection.Emit中使用的想法是正确的。

我最终不得不为开源项目(Autofac)做到这一点,并提出了这种扩展方法:

public static CustomAttributeBuilder ToAttributeBuilder(this CustomAttributeData data)
{
  if (data == null)
  {
    throw new ArgumentNullException("data");
  }

  var constructorArguments = new List<object>();
  foreach (var ctorArg in data.ConstructorArguments)
  {
    constructorArguments.Add(ctorArg.Value);
  }

  var propertyArguments = new List<PropertyInfo>();
  var propertyArgumentValues = new List<object>();
  var fieldArguments = new List<FieldInfo>();
  var fieldArgumentValues = new List<object>();
  foreach (var namedArg in data.NamedArguments)
  {
    var fi = namedArg.MemberInfo as FieldInfo;
    var pi = namedArg.MemberInfo as PropertyInfo;

    if (fi != null)
    {
      fieldArguments.Add(fi);
      fieldArgumentValues.Add(namedArg.TypedValue.Value);
    }
    else if (pi != null)
    {
      propertyArguments.Add(pi);
      propertyArgumentValues.Add(namedArg.TypedValue.Value);
    }
  }
  return new CustomAttributeBuilder(
    data.Constructor,
    constructorArguments.ToArray(),
    propertyArguments.ToArray(),
    propertyArgumentValues.ToArray(),
    fieldArguments.ToArray(),
    fieldArgumentValues.ToArray());
}

该帐户考虑了初始化属性的所有方法。

答案 1 :(得分:2)

这是我在经过一些研究后得出的答案。

CustomAttributeBuilder ct = AddAttributesToMemberInfo(methodInfo);
if (ct != null)
{
    methodBuilder.SetCustomAttribute(ct);
}

CustomAttributeBuilder AddAttributesToMemberInfo(MemberInfo oldMember)
{
    CustomAttributeBuilder ct = null;
    IList<CustomAttributeData> customMethodAttributes = CustomAttributeData.GetCustomAttributes(oldMember);
    foreach (CustomAttributeData att in customMethodAttributes)
    {
        List<object> namedFieldValues = new List<object>();
        List<FieldInfo> fields = new List<FieldInfo>();
        List<object> constructorArguments = new List<object>();
        foreach (CustomAttributeTypedArgument cata in att.ConstructorArguments)
        {
            constructorArguments.Add(cata.Value);
        }
        if (att.NamedArguments.Count > 0)
        {
            FieldInfo[] possibleFields = att.GetType().GetFields();

            foreach (CustomAttributeNamedArgument cana in att.NamedArguments)
            {
                for (int x = 0; x < possibleFields.Length; x++)
                {
                    if (possibleFields[x].Name.CompareTo(cana.MemberInfo.Name) == 0)
                    {
                        fields.Add(possibleFields[x]);
                        namedFieldValues.Add(cana.TypedValue.Value);
                    }
                }


            }
        }

        if (namedFieldValues.Count > 0)
        {
            ct = new CustomAttributeBuilder(att.Constructor, constructorArguments.ToArray(), fields.ToArray(), namedFieldValues.ToArray());
        }
        else
        {
            ct = new CustomAttributeBuilder(att.Constructor, constructorArguments.ToArray());
        }


    }
    return ct;
}

答案 2 :(得分:1)

Travis Illig的代码需要修改如下,以便与.Net Core一起使用:

foreach (var namedArg in data.NamedArguments)
{
  string argName = namedArg.MemberName;
  var fi = data.AttributeType.GetField(argName);
  var pi = data.AttributeType.GetProperty(argName);

答案 3 :(得分:0)

试试这个:

MethodInfo mi;
//...
object[] custAttribs = mi.GetCustomAttributes(false);
foreach (object attrib in custAttribs)
   attrib.GetType();

我假设你的方法有MethodInfo