System.Type 类对于反射起着核心的作用。当反射请求加载的类型时,公共语言运行库将为它创建一个
Type。您可以使用 Type 对象的方法、字段、属性和嵌套类来查找有关该类型的所有信息。
大家运行一下下面的代码根据结果分析一下就能比较清楚的理解Type了
获取类型信息
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MyClass m = new MyClass();
Type type = m.GetType();
Console.WriteLine("类型名:" + type.Name);
Console.WriteLine("类全名:"+type.FullName);
Console.WriteLine("命名空间名:"+type.Namespace);
Console.WriteLine("程序集名:"+type.Assembly);
Console.WriteLine("模块名:"+type.Module);
Console.WriteLine("基类名:"+type.BaseType);
Console.WriteLine("是否类:"+type.IsClass);
Console.WriteLine("类的公共成员:");
MemberInfo[] memberInfos = type.GetMembers();//得到所有公共成员
foreach (var item in memberInfos)
{
Console.WriteLine("{0}:{1}",item.MemberType,item);
}
}
}
class MyClass
{
public string m;
public void test()
{ }
public int MyProperty { get; set; }
namespace ConsoleApplication2
{
public class Example
{
private int factor;
public Example(int f)
{
factor = f;
}
public int SampleMethod(int
x)
{
Console.WriteLine("\nExample.SampleMethod({0}) executes.",
x);
return x * factor;
}
public static void Main()
{
//获取当前执行代码的程序集
Assembly assem = Assembly.GetExecutingAssembly();
Console.WriteLine("Assembly
Full Name:");
Console.WriteLine(assem.FullName);
// The AssemblyName type
can be used to parse the full name.
AssemblyName assemName = assem.GetName();
Console.WriteLine("\nName: {0}", assemName.Name);
Console.WriteLine("Version: {0}.{1}",
assemName.Version.Major, assemName.Version.Minor);
Console.WriteLine("\nAssembly CodeBase:");
Console.WriteLine(assem.CodeBase);
// 从程序集众创建一个Example实例并且用object类型的引用o指向它,同时调用一个输入参数的构造函数
Object o = assem.CreateInstance("ConsoleApplication2.Example",
false,
BindingFlags.ExactBinding,
null, new Object[] { 2 }, null, null);
//构造Example类的一个晚绑定的方法SampleMethod
MethodInfo m = assem.GetType("ConsoleApplication2.Example").GetMethod("SampleMethod");
//调用刚才实例化好的Example对象o中的SampleMethod方法,传入的参数为42
Object ret = m.Invoke(o, new Object[] { 42 });
Console.WriteLine("SampleMethod returned {0}.",
ret);
[Table("User")]
public class User
{
[Colum("userID", DbType = DbType.Int32)]
public int UserID { get; set; }
[Colum("UserName", DbType = DbType.String)]
public string UserName { get; set; }
}
表特性
[AttributeUsageAttribute(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class TableAttribute : Attribute
{
//保存表名的字段
private string _tableName;
public TableAttribute()
{
}
public TableAttribute(string
tableName)
{
this._tableName = tableName;
}
///
/// 映射的表名(表的全名:模式名.表名)
///
public string TableName
{
set
{
this._tableName = value;
}
get
{
return this._tableName;
}
}
}
列特性:
[AttributeUsageAttribute(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class ColumAttribute : Attribute
{
private string _columName;
private DbType _dbType ;
public ColumAttribute()
{
}
public ColumAttribute(string
columName)
: this()
{
this._columName = columName;
}
TextFileLog
class TextFileLog : ILog
{
public bool Write(string message)
{
string fileDir = ConfigurationManager.AppSettings["LogTarget"].ToString();
using (StreamWriter w = File.AppendText(fileDir))
{
// w.Write(" Log Entry : ");
w.WriteLine("发生时间{0}", DateTime.Now.ToLocalTime().ToString());
w.WriteLine("日志内容为:{0}", message);
w.WriteLine("-------------------------------");
// Update the underlying file.
w.Flush();
w.Close();
}
return true;
}
public bool Write(Exception ex)
{
Write(ex.Message);
return true;
}
}
XmlFileLog
class XmlFileLog : ILog
{
public bool Write(string message)
{
string xmlFilePath = ConfigurationManager.AppSettings["LogTarget"].ToString();
if (File.Exists(xmlFilePath))
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
XmlNode nod = doc.SelectSingleNode("Logs");
docFrag.InnerXml = "" + message + "";
nod.AppendChild(docFrag);
doc.Save(xmlFilePath);
return true;
}
else
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true; //设置缩进
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = " ";
settings.OmitXmlDeclaration = false;
using (XmlWriter writer = XmlWriter.Create(xmlFilePath, settings))
{
//Start writing the XML document
writer.WriteStartDocument(false);
//Start with the root element
writer.WriteStartElement("Logs");
writer.WriteStartElement("Log");
writer.WriteStartElement("Time");
writer.WriteString(DateTime.Now.ToLocalTime().ToString());
writer.WriteEndElement();
writer.WriteStartElement("Message");
writer.WriteString(message);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
//Flush the object and write the XML data to the file
writer.Flush();
return true;
}
}
}
public bool Write(Exception ex)
{
Write(ex.Message);
return true;
Path.Combine(Environment.CurrentDirectory,
"LogClassLibrary.dll");
Assembly a = Assembly.LoadFrom(assemblyPath);
Type type = a.GetType(ConfigurationManager.AppSettings["LogType"].ToString());
LogClassLibrary.ILog log =