/// <summary> /// 按类型所在程序集注册所有 /// </summary> /// <param name="currentTypes"></param> /// <returns></returns> public ServerCreater RegisterAll(params Type[] currentTypes) { foreach (var currentType in currentTypes) { var assembyle = System.Reflection.Assembly.GetAssembly(currentType); var types = assembyle.GetTypes(); foreach (var type in types) { if (type.IsSubclassOf(typeof(AbsService))) { var theFilter = new System.Reflection.TypeFilter(MyInterfaceFilter); var implementedInterfaces = type.FindInterfaces(theFilter, type.BaseType).FirstOrDefault(); if (implementedInterfaces == null) { continue; } //实现注册 var mainType = this.GetType(); var method = mainType.GetMethod(nameof(Register), BindingFlags.Public | BindingFlags.Instance); method.MakeGenericMethod(new Type[] { implementedInterfaces, type }).Invoke(this, new object[] { }); } } } return(this); }
public static void AddDynamicApi(this IServiceCollection services, Action <ServerCreater> setupAction, params Assembly[] assemblies) { _setupAction = setupAction; _assemblies = assemblies; services.AddSingleton <ServerCreater>(); foreach (var assembyle in assemblies) { var types = assembyle.GetTypes(); foreach (var type in types) { if (type.IsSubclassOf(typeof(AbsService))) { var theFilter = new System.Reflection.TypeFilter(MyInterfaceFilter); var implementedInterfaces = type.FindInterfaces(theFilter, type.BaseType).FirstOrDefault(); if (implementedInterfaces == null) { continue; } //注册AbsService services.AddScoped(implementedInterfaces, type); } } } }
public static Type CreateBlittableType(Type hostType, bool declare_parent_chain, bool declare_flatten_structure) { try { String name; SR.TypeFilter tf; Type bbt = null; // Declare inheritance types. if (declare_parent_chain) { // First, declare base type Type bt = hostType.BaseType; if (bt != null && !bt.FullName.Equals("System.Object")) { bbt = CreateBlittableType(bt, declare_parent_chain, declare_flatten_structure); } } name = hostType.FullName; tf = new SR.TypeFilter((Type t, object o) => { return(t.FullName == name); }); // Find if blittable type for hostType was already performed. Data data = new Data(); Type[] types = data.mb.FindTypes(tf, null); // If blittable type was not created, create one with all fields corresponding // to that in host, with special attention to arrays. if (types.Length == 0) { if (hostType.IsArray) { // Recurse Type elementType = CreateBlittableType(hostType.GetElementType(), declare_parent_chain, declare_flatten_structure); object array_obj = Array.CreateInstance(elementType, 0); Type array_type = array_obj.GetType(); TypeBuilder tb = null; if (bbt != null) { tb = data.mb.DefineType( array_type.Name, SR.TypeAttributes.Public | SR.TypeAttributes.SequentialLayout | SR.TypeAttributes.Serializable, bbt); } else { tb = data.mb.DefineType( array_type.Name, SR.TypeAttributes.Public | SR.TypeAttributes.SequentialLayout | SR.TypeAttributes.Serializable); } return(tb.CreateType()); } else if (Campy.Types.Utils.ReflectionCecilInterop.IsStruct(hostType) || hostType.IsClass) { TypeBuilder tb = null; if (bbt != null) { tb = data.mb.DefineType( name, SR.TypeAttributes.Public | SR.TypeAttributes.SequentialLayout | SR.TypeAttributes.Serializable, bbt); } else { tb = data.mb.DefineType( name, SR.TypeAttributes.Public | SR.TypeAttributes.SequentialLayout | SR.TypeAttributes.Serializable); } Type ht = hostType; while (ht != null) { var fields = ht.GetFields( SR.BindingFlags.Instance | SR.BindingFlags.NonPublic | SR.BindingFlags.Public | SR.BindingFlags.Static); var fields2 = ht.GetFields(); foreach (var field in fields) { if (field.FieldType.IsArray) { // Convert byte, int, etc., in host type to pointer in blittable type. // With array, we need to also encode the length. tb.DefineField(field.Name, typeof(IntPtr), SR.FieldAttributes.Public); tb.DefineField(field.Name + "Len0", typeof(Int32), SR.FieldAttributes.Public); } else { // For non-array type fields, just define the field as is. tb.DefineField(field.Name, field.FieldType, SR.FieldAttributes.Public); } } if (declare_flatten_structure) { ht = ht.BaseType; } else { ht = null; } } // Base type will be used. return(tb.CreateType()); } else { return(null); } } else { return(types[0]); } } catch { return(null); } }
public static Type CreateBlittableTypeMono(Mono.Cecil.TypeReference hostType, bool declare_parent_chain) { try { Mono.Cecil.TypeDefinition td = hostType.Resolve(); String name; SR.TypeFilter tf; // Declare parent chain since TypeBuilder works top down not bottom up. if (declare_parent_chain) { name = hostType.FullName; name = name.Replace('+', '.'); tf = new SR.TypeFilter((Type t, object o) => { return(t.FullName == name); }); } else { name = hostType.Name; tf = new SR.TypeFilter((Type t, object o) => { return(t.Name == name); }); } // Find if blittable type for hostType was already performed. Data data = new Data(); Type[] types = data.mb.FindTypes(tf, null); // If blittable type was not created, create one with all fields corresponding // to that in host, with special attention to arrays. if (types.Length == 0) { if (hostType.IsArray) { // Recurse Type elementType = CreateBlittableTypeMono(hostType.GetElementType(), true); object array_obj = Array.CreateInstance(elementType, 0); Type array_type = array_obj.GetType(); TypeBuilder tb = null; tb = data.mb.DefineType( array_type.Name, SR.TypeAttributes.Public | SR.TypeAttributes.Sealed | SR.TypeAttributes.SequentialLayout | SR.TypeAttributes.Serializable, typeof(ValueType)); return(tb.CreateType()); } else if (Campy.Types.Utils.ReflectionCecilInterop.IsStruct(hostType) || !hostType.IsValueType) { TypeBuilder tb = null; tb = data.mb.DefineType( name, SR.TypeAttributes.Public | SR.TypeAttributes.Sealed | SR.TypeAttributes.SequentialLayout | SR.TypeAttributes.Serializable, typeof(ValueType)); var fields = td.Fields; foreach (var field in fields) { if (field.FieldType.IsArray) { // Convert byte, int, etc., in host type to pointer in blittable type. // With array, we need to also encode the length. tb.DefineField(field.Name, typeof(IntPtr), SR.FieldAttributes.Public); tb.DefineField(field.Name + "Len0", typeof(Int32), SR.FieldAttributes.Public); } else { // For non-array type fields, just define the field as is. tb.DefineField(field.Name, Campy.Types.Utils.ReflectionCecilInterop.ConvertToSystemReflectionType(field.FieldType), SR.FieldAttributes.Public); } } return(tb.CreateType()); } else { return(null); } } else { return(types[0]); } } catch { return(null); } }
public virtual System.Type[] FindTypes(System.Reflection.TypeFilter filter, object filterCriteria) { throw null; }