using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
namespace testbaseclass
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class ydhdescAttribute : Attribute
{
public ydhdescAttribute(String Description_in)
{
this.description = Description_in;
}
protected String description;
public String Description
{
get
{
return this.description;
}
}
}
class Program
{
[DllImport("User32.dll")]
public static extern int MessageBox(int hParent, string Message, string Caption, int Type);
static void Main(string[] args)
{
zi bb = new zi();
fu a = null;
a = new zi();
Type myType = a.GetType();
myType.InvokeMember("zi1",BindingFlags.SetProperty, null, a, new object[]{"sdfsdf"});
string age = (string)myType.InvokeMember("zi1", BindingFlags.GetProperty, null, a, null);
MemberInfo[] mis=a.GetType().GetMembers();
foreach (MemberInfo item in mis)
{
Attribute[] attrs = Attribute.GetCustomAttributes(item);
if (attrs.Length > 0 && (attrs[0] is ydhdescAttribute))
{
if ((attrs[0] as ydhdescAttribute).Description == "one")
{
myType.InvokeMember("zi1", BindingFlags.SetProperty, null, a, new object[] { "sdfsdf" });
Console.WriteLine((string)myType.InvokeMember("zi1", BindingFlags.GetProperty, null, a, null));
}
}
}
//foreach (Attribute attr in attrs)
//{
// if (attr is ydhdescAttribute)
// {
// ydhdescAttribute help = (ydhdescAttribute)attr;
// Console.WriteLine(help.Description);
// }
//}
Console.ReadLine();
}
}
interface fu
{
string a();
}
class zi:fu
{
string fu.a()
{
return "";
}
[ydhdesc("one")]
public string zi1 { get; set; }
}
}
Attribute + Reflection
最新推荐文章于 2018-08-15 20:43:53 发布