using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.Configuration;
namespace Reflection
{
class Program
{
static void Main(string[] args)
{
#region core下load方法有问题,先执行LoadFrom,再执行load方法又好了,不知道原理
//core下load方法有问题,先执行LoadFrom,再执行load方法又好了,不知道原理
//Assembly assembly = Assembly.Load(@"MySqlHelper");
//Assembly assembly1 = Assembly.LoadFile(@"C:\Users\王晨\source\repos\Wchentest\ConsoleApp1\bin\Debug\netcoreapp3.1\MySqlHelper.dll");
//Assembly assembly3 = Assembly.LoadFrom(@"C:\Users\王晨\source\repos\Wchentest\ConsoleApp1\bin\Debug\netcoreapp3.1\MySqlHelper.dll");
#endregion
#region 把反射封装了下,用到了core再json中配置
//引用两个包Microsoft.Extensions.Configuration;Microsoft.Extensions.Configuration.josn;
//Console.WriteLine($"{SimpleFactory.GetStr()}");
#endregion
#region 反射获取构造函数,可以获得每个构造函数,和参数类型,名称
{
反射获取构造函数,可以获得每个构造函数,和参数类型,名称
//Assembly assembly2 = Assembly.LoadFrom("MySqlHelper.dll");
//Type type = assembly2.GetType("MySqlHelper.MySql");
//foreach (ConstructorInfo ctor in type.GetConstructors())
//{
// Console.WriteLine(ctor.Name);
// foreach (var parameter in ctor.GetParameters())
// {
// Console.WriteLine(parameter.ParameterType + parameter.Name);
// }
//}
调用不同的构造参数实例化
//object omysql = Activator.CreateInstance(type);
//object omysql1 = Activator.CreateInstance(type, new object[] { "123" });
//object omysql2 = Activator.CreateInstance(type, new object[] { 1 });
//object omysql3 = Activator.CreateInstance(type, new object[] { 1, "123" });
}
#endregion
#region 验证下单例,反射破坏单例
{
验证下单例
//Singleton singleton0 = Singleton.GetSingleton();
//Singleton singleton1 = Singleton.GetSingleton();
//Singleton singleton2 = Singleton.GetSingleton();
//Console.WriteLine(singleton0.Equals(singleton1));
//Console.WriteLine(object.ReferenceEquals(singleton0, singleton1));
反射破环单例
//Assembly assembly2 = Assembly.LoadFrom("MySqlHelper.dll");
//Type type = assembly2.GetType("MySqlHelper.Singleton");
第二个参数要是true才能进私有构造函数
//object osingletona = Activator.CreateInstance(type,true);//第一次会调用两次构造函数
//object osingletonb = Activator.CreateInstance(type, true);
//object osingletonc = Activator.CreateInstance(type, true);
//object osingletond = Activator.CreateInstance(type, true);
//Console.WriteLine(osingletona.Equals(osingletonb));
//Console.WriteLine(object.ReferenceEquals(osingletonc, osingletonb));
}
#endregion
#region 反射调用重载方法,静态方法和私有方法,ref和out反射得注意下,不能有参数个数类型相同的普通重载方法
{
//Assembly assembly = Assembly.LoadFrom("MySqlHelper.dll");
//Type type = assembly.GetType("MySqlHelper.Test");
//object oTest = Activator.CreateInstance(type);
//{
// //无参数
// MethodInfo method = type.GetMethod("show", new Type[] { });
// method.Invoke(oTest, null);
// //int
// MethodInfo method1 = type.GetMethod("show", new Type[] { typeof(int) });
// method1.Invoke(oTest, new object[] { 12 });
// //string
// MethodInfo method2 = type.GetMethod("show", new Type[] { typeof(string) });
// method2.Invoke(oTest, new object[] { "wangchen" });
// //int ,string
// MethodInfo method3 = type.GetMethod("show", new Type[] { typeof(int), typeof(string) });
// method3.Invoke(oTest, new object[] { 12, "wangchen" });
// //string ,int
// MethodInfo method4 = type.GetMethod("show", new Type[] { typeof(string), typeof(int) });
// method4.Invoke(oTest, new object[] { "wangchen", 12 });
//}
静态方法,可传实例,也可不传
//{
// MethodInfo method = type.GetMethod("show2");
// method.Invoke(oTest, null);
// MethodInfo method1 = type.GetMethod("show2");
// method1.Invoke(null, null);
//}
私有需要加属性BindingFlags.Instance | BindingFlags.NonPublic
//{
// MethodInfo method = type.GetMethod("show1", BindingFlags.Instance | BindingFlags.NonPublic);
// method.Invoke(oTest, null);
//}
//{
// //ref和out的方法调用,但是如果有参数个数和类型相同的重载普通方法,就反射不到ref或者out方法了
// //默认找的普通方法
// foreach (MethodInfo item in type.GetMethods())
// {
// }
// MethodInfo method = type.GetMethod("show2", new Type[] { typeof(string) });
// object[] oarr = new object[] { "refwww" };
// method.Invoke(oTest, oarr);
// //接收out出来的值
// Console.WriteLine(oarr[0]);
//}
}
#endregion
#region 反射调用泛型类和方法,反射调用泛型重载方法不知道怎么搞
{
//Assembly assembly = Assembly.LoadFrom("MySqlHelper.dll");
//Type type = assembly.GetType("MySqlHelper.Genemic`1")
// .MakeGenericType(new Type[] { typeof(int) });
//object ogenemic = Activator.CreateInstance(type);
//foreach (MethodInfo method_ in type.GetMethods())
//{
// Console.WriteLine(method_.Name + ":" + method_.IsGenericMethod);
//}
//MethodInfo method = type.GetMethod("Output")
// .MakeGenericMethod(new Type[] { typeof(string), typeof(DateTime) });
//method.Invoke(ogenemic, new object[] { 123, "wangchen", DateTime.Now });
//MethodInfo method1 = type.GetMethod("Output1")
// .MakeGenericMethod(new Type[] { typeof(string), typeof(DateTime) });
//method1.Invoke(ogenemic, new object[] { "wangchen", DateTime.Now });
}
#endregion
#region 接口 抽象类
{
如果需要父类,给的是子类,那么相同一般方法(不包括虚方法virtual)调用的是父类的
//Animal animal = new dogs();
//animal.run();
可以在子类相同的方法中用base.function调用父类的方法
//dogs dog = new dogs();
//dog.run();
实例化子类,一定会走父类的构造方法,可以用:base()来确定调哪个父类构造方法
重写-new 覆写父类virtual,子类override 重载
接口不能存在字段,子类,委托(core中虽然可以写子类和委托,但没什么意义) 属性,时间,索引可以存在
//pig pig = new pig();
var ff = new pig.child();
ff.GetChild();
}
#endregion
#region 作业
{
User user = new User() { Id = 123, UserName = "小高" };
//静态扩展方法
user.GetProperty();
//DBHelper.GetProperty(user);
var user1 = DBHelper.GetData<User>(1);
var lst = DBHelper.GetData<User>();
}
#endregion
}
}
}
单例
using System;
using System.Collections.Generic;
using System.Text;
namespace Reflection
{
public sealed class Singleton
{
/*单例四要素:
1.私有静态属性,类型是本对象
2.私有构造函数
3.静态构造函数,实例化本对象赋值给私有静态属性
4.提供给外部的方法,返回私有静态属性
*/
//反射会破环单例--反射可以调用私有构造函数
private static Singleton singleton = null;
static Singleton()
{
singleton = new Singleton();
}
private Singleton()
{
Console.WriteLine("私有构造函数被创建");
}
public static Singleton GetSingleton()
{
return singleton;
}
}
}
using Microsoft.Extensions.Configuration;
using SimpleFactory;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Reflection
{
//core读配置的简单工厂
public class SimpleFactory
{
public static string GetStr()
{
var configuration = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build();
string str = configuration["IDBconfig"];
Assembly assembly2 = Assembly.LoadFrom(configuration["IDBconfig"].Split(',')[0]);
Assembly assembly = Assembly.Load(configuration["IDBconfig"].Split(',')[0]);
Type type = assembly.GetType(configuration["IDBconfig"].Split(',')[1]);
object oDBHelper = Activator.CreateInstance(type);
IBInterFace iDBHelper = oDBHelper as IBInterFace;
return iDBHelper.GetMysqlStr("core123");
}
}
}