下面是一些完全工作的代碼,包括可選調用參數:
private static string Call(object callByAttribute, string name, object[] args)
{
PropertyInfo prop = callByAttribute.GetType().GetProperties()
.Where(p => p.IsDefined(typeof(CodeNameAttribute), false))
.SingleOrDefault(p => ((CodeNameAttribute)(p.GetCustomAttributes(typeof(CodeNameAttribute), false)).First()).Name == name);
if (prop != null)
return (string)callByAttribute.GetType().InvokeMember(prop.Name, BindingFlags.GetProperty, null, callByAttribute, null);
MethodInfo method = callByAttribute.GetType().GetMethods()
.Where(p => p.IsDefined(typeof(CodeNameAttribute), false))
.SingleOrDefault(p => ((CodeNameAttribute)(p.GetCustomAttributes(typeof(CodeNameAttribute), false)).First()).Name == name);
if (method != null)
return (string)callByAttribute.GetType().InvokeMember(method.Name, BindingFlags.InvokeMethod, null, callByAttribute, args);
throw new Exception("method/getter not found");
}
private static string Call(object callByAttribute, string name)
{
return Call(callByAttribute, name, null);
}
這可以在一個完整的程序中使用這樣的:
using System;
using System.Linq;
using System.Reflection;
namespace ConsoleApplication1
{
public class CallByAttribute
{
[CodeName("Foo")]
public string MyProperty { get; set; }
[CodeName("Bar")]
public string MyMethod(int someParameter)
{
return "blah" + someParameter;
}
}
public class CodeNameAttribute : Attribute
{
private readonly string name;
public CodeNameAttribute(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
}
}
class Program
{
static void Main(string[] args)
{
CallByAttribute callByAttribute = new CallByAttribute();
callByAttribute.MyProperty = "hi";
Console.WriteLine(Call(callByAttribute, "Bar", new object[] {1}));
Console.WriteLine(Call(callByAttribute, "Foo"));
}
private static string Call(object callByAttribute, string name)
{
return Call(callByAttribute, name, null);
}
private static string Call(object callByAttribute, string name, object[] args)
{
PropertyInfo prop = callByAttribute.GetType().GetProperties()
.Where(p => p.IsDefined(typeof(CodeNameAttribute), false))
.SingleOrDefault(p => ((CodeNameAttribute)(p.GetCustomAttributes(typeof(CodeNameAttribute), false)).First()).Name == name);
if (prop != null)
return (string)callByAttribute.GetType().InvokeMember(prop.Name, BindingFlags.GetProperty, null, callByAttribute, null);
MethodInfo method = callByAttribute.GetType().GetMethods()
.Where(p => p.IsDefined(typeof(CodeNameAttribute), false))
.SingleOrDefault(p => ((CodeNameAttribute)(p.GetCustomAttributes(typeof(CodeNameAttribute), false)).First()).Name == name);
if (method != null)
return (string)callByAttribute.GetType().InvokeMember(method.Name, BindingFlags.InvokeMethod, null, callByAttribute, args);
throw new Exception("method/getter not found");
}
}
}
看起來你需要'CodeName'的一個伴侶'Attribute'類;這是真的,還是你可以只反映一下屬性來獲取每個的CodeName? – 2010-09-14 04:11:07
Robert:假設你已經有了一個'CodeNameAttribute'類,否則你將無法執行'[CodeName(「foo」)]',因爲這實際上導致了一個'CodeNameAttribute'對象的實例化。 – Gabe 2010-09-14 04:13:30
如果您打算使用這些代碼名(數千次或數百萬次),您需要將屬性查找的結果緩存爲可在字典中查找的代理。 – Gabe 2010-09-14 05:47:29