111,092
社区成员




public string Exec(string mothedName)
{
string ret = "";
switch (mothedName)
{
case "get":
ret = get();
break;
case"get1":
ret = gettest();
break;
//.....
case "testget":
ret = getrset();
break;
}
return ret;
}
public string get()
{
return "get";
}
public string gettest()
{
return "gettest";
}
public string getrset()
{
return "getset";
}
static void Main(string[] args)
{
Console.WriteLine(TestEnum.get.Exec());
Console.Read();
}
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
sealed class TestAttribute : Attribute
{
private IGetExec ExecObj { get;set; }
public TestAttribute(Type t)
{
this.ExecObj = Activator.CreateInstance(t) as IGetExec;
}
public string Exec()
{
return ExecObj.GetResult();
}
}
public interface IGetExec
{
string GetResult();
}
public static string Exec(this TestEnum e)
{
Type t = e.GetType();
var file = t.GetField(e.ToString());
var att = file.GetCustomAttributes(typeof(TestAttribute), false).Single();
if (att != null)
{
return (att as TestAttribute).Exec();
}
else
return null;
}
public enum TestEnum
{
[Test(typeof(GetExec))]
get,
[Test(typeof(Get1Exec))]
getTest,
[Test(typeof(GetTestExec))]
get1
}
static void Main(string[] args)
{
Console.WriteLine(Exec(GetSet));
Console.ReadKey();
}
//定义委托,用于将方法做为参数传给Exec.
public delegate string GetResultDelegate();
public static string Get()
{
return "get";
}
public static string GetTest()
{
return "gettest";
}
public static string GetSet()
{
return "getSet";
}
public static string Exec(GetResultDelegate getResult)
{
return getResult();
}