using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test_20190902_1 { class Program { static void Main(string[] args) { HomeController home = new HomeController(); //創建對象 ; 先執行父類構造方法 } } public class HomeController : BaseController { public HomeController() { Console.WriteLine("HomeController類的構造方法"); // 執行順序 4 }//構造方法 } public class BaseController : Controller { public BaseController() { Console.WriteLine("BaseController類的構造方法"); // 執行順序 3 }//構造方法 LogManager logdb = new LogManager(); //先執行屬性,再 執行構造 } //抽象類 public abstract class Controller {} public class LogManager : DbContext<A> { public LogManager() { Console.WriteLine("LogManager類的構造方法"); // 執行順序 2 }//構造方法 } public class DbContext<T> where T : class { public DbContext() { Console.WriteLine("DbContext類的構造方法"); // 執行順序 1 }//構造方法 //Virtual方法(虛方法) public virtual List<T> GetList() { return new List<T>(); } } //public class DbContext<T> where T : class, new() //{ // public DbContext() { // Console.WriteLine("DbContext類的構造方法"); // 執行順序 1 // }//構造方法 // //Virtual方法(虛方法) // public virtual List<T> GetList() // { // return new List<T>(); // } //} public class A { } //加new 和不加new 有什么不同? }
//----------
//不包含空構造函數 時候 ,編譯不通過
public class A {
public A()
{
}
public A(string str)
{
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test_20190902_2 { class Program { static void Main(string[] args) { UserManager userdb = new UserManager(); //創建對象 var t = userdb.GetFirst("test"); //繼承類可以使用父類方法 } } public class A{ int z1 { get; set; } int z2 { get; set; } } public class UserManager : DbContext<A> { } // T是類型 public class DbContext<T> where T : class { public object Db;//用來處理事務多表查詢和復雜的操作 public DbContext(){} public virtual T GetFirst(string str) { //return default(T);等同於 return null; return null; // Db.Queryable<T>().First(whereExpression); //return new T(); //T是不同類,要返回不同類的對象時這種方法錯誤 } } }
public static void test<T>(T A, T B) where T : class, new() { T temp = new T(); object value; System.Reflection.PropertyInfo[] obj1_s = A.GetType().GetProperties(); //獲得該對象所有屬性名 for (int i = 0; i < obj1_s.Length; i++) { //temp =A; value = obj1_s[i].GetValue(A); //獲得 第一個對象的 第一個屬性值 obj1_s[i].SetValue(temp, value, null); //設置 第二個對象的 第一個屬性值 // A = B value = obj1_s[i].GetValue(B); obj1_s[i].SetValue(A, value, null); //設置 第二個對象的 第一個屬性值 //B =temp value = obj1_s[i].GetValue(temp); obj1_s[i].SetValue(B, value, null); //設置 第二個對象的 第一個屬性值 } }
public class ABC { int abc { get; set; } } void fun3() { fun2<ABC>(new ABC()); } void fun2<T>(T abc) where T : class,new() { fun1<T>(abc); } void fun1<T>(T abc) where T:class,new() { abc = new T(); }