一、反射
-
反射加载获取类的元数据
-
A类
//反射
class A
{
public string MyLast { get; set; }
public void MyTest(int a)
{
Console.WriteLine(a);
}
public void A1()
{
Console.WriteLine("A1......");
}
public void A2()
{
Console.WriteLine("A2......");
}
}
- 测试类
var input = Console.ReadLine();
/*
* 使用反射的机制,拿到实例A的源数据
*/
var type = typeof(A);
var a = new A();
var prop = type.GetProperty("MyLast");
prop.SetValue(a,"aaa",null);
//获取方法
//input使用输入的方式选择那个方法
var method = type.GetMethod(input);
method.Invoke(a, null);
Console.WriteLine(a.MyLast);
- 使用程序集反射
- 接口
interface IMethods
{
public void A1();
public void A2();
}
- A类
class A: IMethods
{
public string MyLast { get; set; }
public void MyTest(int a)
{
Console.WriteLine(a);
}
public void A1()
{
Console.WriteLine("A:A1......");
}
public void A2()
{
Console.WriteLine("A:A2......");
}
}
- B类
class B : IMethods
{
public string MyLast { get; set; }
public void MyTest(int a)
{
Console.WriteLine(a);
}
public void A1()
{
Console.WriteLine("B:A1......");
}
public void A2()
{
Console.WriteLine("B:A2......");
}
}
- 测试
static void Main(string[] args)
{
var input = Console.ReadLine();
var input2 = Console.ReadLine();
/*
* 使用反射的机制,拿到实例A的源数据
* Demo02程序集名称 Demo02.类型 这里使用input选择输入
*/
var a = Assembly.Load("Demo02").CreateInstance("Demo02." + input);
//获取方法
var method = a.GetType().GetMethod(input2);
method.Invoke(a, null);
//Console.WriteLine(a.MyLast);
Console.ReadLine();
}
二、特性
- Required特性[必须存在]
//实现Attribute
//只能修饰属性
[AttributeUsage(AttributeTargets.Property)]
class MyRequired : System.Attribute
{
/// <summary>
/// 检查Required是否为null
/// </summary>
/// <param name="obj">参数</param>
/// <returns>trur,false</returns>
public static bool MyRequiredIsNull(object obj)
{
//获取类型
var type = obj.GetType();
//获取属性
var propertys = type.GetProperties();
//循环遍历
foreach(var property in propertys)
{
var attr = property.GetCustomAttributes(typeof(MyRequired),false);
if (attr.Length >0 )
{
if(property.GetValue (obj,null) == null)
{
return false;
}
}
}
return true;
}
}
- 测试
var a = new A();
if (MyRequired.MyRequiredIsNull(a))
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
三、序列化
- 在类上加【Serializable】表示序列号对象
[Serializable]
class A{
public string MyLast { get; set; }
public void MyTest(int a)
{
Console.WriteLine(a);
}
}
- 测试
var a = new A() { MyLast = "123"};
//.bin 序列化文件后缀
using (var stram = File.Open(typeof(A).Name + ".bin", FileMode.Create))
{
var bf = new BinaryFormatter();
//流 对象
bf.Serialize(stram,a);
}
//反序列化
using (var starm = File.Open(typeof(A).Name + ".bin", FileMode.Open))
{
var bf = new BinaryFormatter();
bf.Deserialize(starm);
}
Console.WriteLine(a.MyLast);
四、动态编程
- XML解析类
public class DynamicXml : DynamicObject, IEnumerable
{
private readonly List<XElement> _elements;
public DynamicXml(string text)
{
var doc = XDocument.Parse(text);
_elements = new List<XElement> { doc.Root };
}
protected DynamicXml(XElement element)
{
_elements = new List<XElement> { element };
}
protected DynamicXml(IEnumerable<XElement> elements)
{
_elements = new List<XElement>(elements);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;
if (binder.Name == "Value")
result = _elements[0].Value;
else if (binder.Name == "Count")
result = _elements.Count;
else
{
var attr = _elements[0].Attribute(
XName.Get(binder.Name));
if (attr != null)
result = attr;
else
{
var items = _elements.Descendants(
XName.Get(binder.Name));
if (items == null || items.Count() == 0)
return false;
result = new DynamicXml(items);
}
}
return true;
}
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
int ndx = (int)indexes[0];
result = new DynamicXml(_elements[ndx]);
return true;
}
public IEnumerator GetEnumerator()
{
foreach (var element in _elements)
yield return new DynamicXml(element);
}
}
- 解析XML
string xml = @"
<books>
<book>
<title>123</title>
</book>
</books>
";
dynamic aa = new DynamicXml(xml);
Console.WriteLine(aa.book[0].title.Value);
//两种方式选择一种即可
foreach (dynamic b in aa.book)
{
Console.WriteLine("title:{0}", b.title.Value);
}
Console.ReadLine();