using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Class1;
using service;
using mode;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Dynamic;
namespace WebApplication3.Controllers
{
public class DllController : Controller
{
mode.Class1 s = new mode.Class1();
// GET: Dll
public ViewResult Index()
{
Console.WriteLine("12345");
Class2 c = new Class2();
//调用controller的class2
ViewBag.Greeting = c.m();
if (c.m() == "s")
{
ViewBag.Greeting = "操作成功" + c.m();
return View();
}
else
{
ViewBag.Greeting = "操作失败" + c.m();
return View();
}
}
public ViewResult Index2()
{
int a1 = 1;
int[] a = new int[11];
//利用反射
var pro = typeof(DllController);
var proq = pro.GetProperty("my");
proq.SetValue(a, "ss");
//利用反射获得实例
var instance = Assembly.Load("DllController").CreateInstance("DllController");
var fun = instance.GetType().GetMethod(my);
return null;
}
//执行方法前检测↓
[Required]
public string my { get; set; }
public void a() { }
}
//检测方法
public class RequiredAttribute : System.Attribute
{
public static bool ISpr()
{
var type = Assembly.Load("DllController").CreateInstance("DllController");
var fun = type.GetType().GetMethod(my);
return true;
}
}
public class b
{
public bool check()
{
var asss = new b2();
//序列化,流
using (var steam = File.Open(typeof(b2).Name + ".bin", FileMode.Create))
{
var bf = new BinaryFormatter();
bf.Serialize(steam, asss);
}
//反序列化
using (var steam = File.Open(typeof(b2).Name + ".bin", FileMode.Open))
{
var bf = new BinaryFormatter();
bf.Deserialize(steam);
}
return false;
}
}
//加上这个,序列化生成文件b2
[Serializable]
public class b2
{
}
public class xxx : DynamicObject
{
public void xml()
{
string xml =@" <books> <book></book> <book> <title>123</title> </book> </books>";
dynamic aaa = DynamicXml.Parse(xml);
System.Diagnostics.Trace.WriteLine(aaa.book[1].title.Value);
}
}
}
动态编程,获取xml的轮子工具DynamicXml
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace WebApplication3.Controllers
{
public class DynamicXml : DynamicObject
{
XElement _root;
private DynamicXml(XElement root)
{
_root = root;
}
public static DynamicXml Parse(string xmlString)
{
return new DynamicXml(XDocument.Parse(xmlString).Root);
}
public static DynamicXml Load(string filename)
{
return new DynamicXml(XDocument.Load(filename).Root);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;
var att = _root.Attribute(binder.Name);
if (att != null)
{
result = att.Value;
return true;
}
var nodes = _root.Elements(binder.Name);
if (nodes.Count() > 1)
{
result = nodes.Select(n => n.HasElements ? (object)new DynamicXml(n) : n.Value).ToList();
return true;
}
var node = _root.Element(binder.Name);
if (node != null)
{
result = node.HasElements || node.HasAttributes ? (object)new DynamicXml(node) : node.Value;
return true;
}
return true;
}
}
}