static void Main() { Attr.Message("IN MAIN"); func1(); //obsolete //Attr.OLD(); //便利Rectangle的属性 Rectangle rect = new Rectangle(10, 25); Type type = typeof(Rectangle); foreach (Object attrs in type.GetCustomAttributes(false)) { DeBugInfo dbi = (DeBugInfo)attrs; if (dbi != null) { Console.WriteLine("BugNo:{0}, Message:{1}", dbi.BugNo, dbi.Message); } } //索引 IndexTest idt = new IndexTest(5); for (int i = 0; i < +5; ++i) { idt[i] = i * 10 + 1; } for (int i = 0; i < idt.size; ++i) { Console.WriteLine(idt[i]); } Console.WriteLine($"STRING:{idt["1"]}"); Console.ReadKey(); }
public void Test01() { Rectangle r = new Rectangle(4.5, 7.5); r.Display(); Type type = typeof(Rectangle); // 遍历 Rectangle 类的特性 foreach (Object attributes in type.GetCustomAttributes(false)) { DeBugInfo dbi = (DeBugInfo)attributes; if (null != dbi) { Console.WriteLine("Bug no: {0}", dbi.BugNo); Console.WriteLine("Developer: {0}", dbi.Developer); Console.WriteLine("Last Reviewed: {0}", dbi.LastReview); Console.WriteLine("Remarks: {0}", dbi.Message); Console.WriteLine("============"); } } Console.WriteLine("-------------"); // 遍历方法特性 foreach (MethodInfo m in type.GetMethods()) { foreach (System.Attribute a in m.GetCustomAttributes(true)) { DeBugInfo dbi = a as DeBugInfo; if (null != dbi) { Console.WriteLine("Bug no: {0}, for Method: {1}", dbi.BugNo, m.Name); Console.WriteLine("Developer: {0}", dbi.Developer); Console.WriteLine("Last Reviewed: {0}", dbi.LastReview); Console.WriteLine("Remarks: {0}", dbi.Message); } else { Console.WriteLine("unknow: " + m + "____" + a); } } } Console.ReadLine(); }
// Start is called before the first frame update void Start() { // Message(new []{"ni","hao","ma"}); // Message("gun"); // #region 反射 特性 #region 遍历类的方法名 foreach (var m in typeof(CSharpTest).GetMethods()) { // // print(m.Name); } #endregion #region 遍历 Rectangle 类的特性 foreach (var attribute in typeof(Rectangle).GetCustomAttributes(false)) { DeBugInfo dbi = (DeBugInfo)attribute; print(dbi.Developer); } #endregion #region 遍历方法的特性 foreach (var method in typeof(Rectangle).GetMethods()) { foreach (var attribute in method.GetCustomAttributes()) { DeBugInfo deBugInfo = (DeBugInfo)attribute; print(deBugInfo.BugNo); } } #endregion #endregion }
public void ExeRectangle() { Rectangle r = new Rectangle(4.5, 7.5); r.Display(); Type type = typeof(Rectangle); //遍历Rectangle类的特性 foreach (var attributes in type.GetCustomAttributes(false)) { DeBugInfo dbi = (DeBugInfo)attributes; if (dbi != null) { WriteLine($"Bug no:{dbi.BugNo}"); WriteLine($"Developer:{dbi.Developer}"); WriteLine($"Last Review:{dbi.LastReview}"); WriteLine($"Remarks:{dbi.Message}"); } } //遍历方法特性 foreach (MethodInfo m in type.GetMethods()) { foreach (Attribute a in m.GetCustomAttributes(true)) { if (a is DeBugInfo) { DeBugInfo dbi = (DeBugInfo)a; if (dbi != null) { WriteLine($"Bug no:{dbi.BugNo}"); WriteLine($"Developer:{dbi.Developer}"); WriteLine($"Last Review:{dbi.LastReview}"); WriteLine($"Remarks:{dbi.Message}"); } } } } }
static void Main(string[] args) { Rectangle r = new Rectangle(4.5, 7.5); r.Display(); Type type = typeof(Rectangle); //iterating through the attribtues of the Rectangle class foreach (Object attributes in type.GetCustomAttributes(false)) { DeBugInfo dbi = (DeBugInfo)attributes; if (null != dbi) { Console.WriteLine("Bug no: {0}", dbi.BugNo); Console.WriteLine("Developer: {0}", dbi.Developer); Console.WriteLine("Last Reviewed: {0}", dbi.LastReview); Console.WriteLine("Remarks: {0}", dbi.Message); } } //iterating through the method attribtues foreach (MethodInfo m in type.GetMethods()) { foreach (Attribute a in m.GetCustomAttributes(true)) { DeBugInfo dbi = a as DeBugInfo; if (null != dbi) { Console.WriteLine("Bug no: {0}, for Method: {1}", dbi.BugNo, m.Name); Console.WriteLine("Developer: {0}", dbi.Developer); Console.WriteLine("Last Reviewed: {0}", dbi.LastReview); Console.WriteLine("Remarks: {0}", dbi.Message); } } } Console.ReadLine(); }
public List <T> Query <T>() { List <T> result = new List <T>(); Type tt = typeof(T); string table = ""; var attrs = tt.GetCustomAttributes(false); foreach (object attr in attrs) { DeBugInfo dbi = (DeBugInfo)attr; if (dbi != null) { table = dbi.Tablename; } } string sql = $"select * from {table}"; //List<OracleParameter> paras = new List<OracleParameter>(); //paras.Add(new OracleParameter(":tname", table)); OracleDataReader dr = base.ExecuteSelect(sql, null); DataTable dt = dr.GetSchemaTable(); Dictionary <string, string> columns = new Dictionary <string, string>(); foreach (DataRow row in dt.Rows) { columns.Add(row["ColumnName"].ToString().ToUpper(), row["DataType"].ToString().ToUpper()); } string classname = Regex.Split((tt.FullName + "," + tt.Assembly.ManifestModule.Name), ".dll")[0]; Type pt = Type.GetType(classname); while (dr.Read()) { object ob = GetObjValueInColumns(dr, columns, pt); result.Add((T)ob); } dr.Close(); return(result); }
static void Main(string[] args) { Rectangle r = new Rectangle(4.5, 7.5); r.Display(); Type type = typeof(Rectangle); // 遍历 Rectangle 类的特性 foreach (Object attributes in type.GetCustomAttributes(false)) { DeBugInfo dbi = (DeBugInfo)attributes; if (null != dbi) { Console.WriteLine("Bug no: {0}", dbi.BugNo); Console.WriteLine("Developer: {0}", dbi.Developer); Console.WriteLine("Last Reviewed: {0}", dbi.LastReview); Console.WriteLine("Remarks: {0}", dbi.Message); } } // 遍历方法特性 foreach (MethodInfo m in type.GetMethods()) { foreach (Attribute a in m.GetCustomAttributes(true)) { try { DeBugInfo dbi = (DeBugInfo)a; if (null != dbi) { Console.WriteLine("Bug no: {0}, for Method: {1}", dbi.BugNo, m.Name); Console.WriteLine("Developer: {0}", dbi.Developer); Console.WriteLine("Last Reviewed: {0}", dbi.LastReview); Console.WriteLine("Remarks: {0}", dbi.Message); } } catch (Exception) { continue; } } } //获取类型信息 Type t = Assembly.Load("ReflactionHepler").GetType("ReflactionHepler.TestClass"); //构造器的参数 object[] constructParams = new object[] { "tummy" }; //根据类型创建对象 object dObj = Activator.CreateInstance(t, constructParams); //获取方法的信息 MethodInfo method = t.GetMethod("GetValue"); //调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值 BindingFlags flag = BindingFlags.Public | BindingFlags.Instance; //GetValue方法的参数 object[] parameters = new object[] { "Hello" }; //调用方法,用一个object接收返回值 object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder, parameters, null); Console.WriteLine(returnValue); TestDelegateClass objTestDelegateClass = new TestDelegateClass(); Type tTestDelegate = Assembly.Load("ReflactionHepler").GetType("ReflactionHepler.TestDelegate"); Type tTestDelegateClass = Assembly.Load("ReflactionHepler").GetType("ReflactionHepler.TestDelegateClass"); TestDelegate methodTestDelegate = (TestDelegate)Delegate.CreateDelegate(tTestDelegate, objTestDelegateClass, "GetValue"); string returnValueTestDelegate = methodTestDelegate("hello"); Console.WriteLine(returnValueTestDelegate); PropertyInfo propertyInfo = tTestDelegateClass.GetProperty("Id"); FieldInfo fieldInfo = tTestDelegateClass.GetField("Name"); // TestDelegateClass objTestDelegateClass2=(TestDelegateClass)tTestDelegateClass.FastNew string name1, name2; name1 = "Jimmy Zhang"; name2 = "张子阳"; DelegateAccess.GreetPeople(name1, DelegateAccess.EnglishGreeting); DelegateAccess.GreetPeople(name2, DelegateAccess.ChineseGreeting); Console.ReadKey(); }