public void Precache() { DynamicMethod method = new DynamicMethod("Db" + Guid.NewGuid().ToString(), typeof(CallModel), new Type[0]); ILGenerator il = method.GetILGenerator(); ConstructorInfo ctor = typeof(CallModel).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null); il.Emit(OpCodes.Newobj, ctor); il.Emit(OpCodes.Ret); EmitFunc = (Func <CallModel>)(method.CreateDelegate(typeof(Func <CallModel>))); NatashaFunc = CtorBuilder.NewDelegate <CallModel>(); }
public static Func <CallerBase> InitType(Type type) { ClassBuilder builder = new ClassBuilder(); StringBuilder body = new StringBuilder(); var fields = type.GetFields(); var props = type.GetProperties(); List <BuilderModel> buildCache = new List <BuilderModel>(fields.Length + props.Length); fields.For(item => buildCache.Add(item)); props.For(item => buildCache.Add(item)); buildCache.Sort(); CallerActionBuilder callerBuilder = new CallerActionBuilder(buildCache, type.GetDevelopName()); body.Append(callerBuilder.GetScript_GetDynamicBase()); body.Append(callerBuilder.GetScript_GetByName()); body.Append(callerBuilder.GetScript_GetByIndex()); body.Append(callerBuilder.GetScript_SetByName()); body.Append(callerBuilder.GetScript_SetByIndex()); Type tempClass = builder .Using(type) .Using("System") .Using("NCaller") .Using("System.Collections.Concurrent") .ClassAccess(AccessTypes.Public) .ClassName("NatashaDynamic" + type.GetAvailableName()) .Namespace("NCallerDynamic") .Inheritance <CallerBase>() .ClassBody(body) .GetType(); return(TypeCreatorMapping[type] = (Func <CallerBase>)CtorBuilder.NewDelegate(tempClass)); }
public static Func <DynamicBase> InitType(Type type) { string innerClassName = "InnerDynamicStatic" + type.GetAvailableName(); string entityClassName = type.GetDevelopName(); string className = "NatashaDynamicStatic" + type.GetAvailableName(); ClassBuilder builder = new ClassBuilder(); StringBuilder body = new StringBuilder(); body.Append($@" public readonly static ConcurrentDictionary<string,Func<DynamicBase>> LinkMapping; static {className}(){{ LinkMapping = new ConcurrentDictionary<string,Func<DynamicBase>>(); var fields = typeof({entityClassName}).GetFields(); for (int i = 0; i < fields.Length; i+=1) {{ if(!fields[i].FieldType.IsOnceType()) {{ LinkMapping[fields[i].Name] = FastMethodOperator .New .Using(fields[i].FieldType) .Using(""Natasha.Caller"") .MethodBody($@""return {entityClassName}.{{fields[i].Name}}.Caller<{{fields[i].FieldType.GetDevelopName()}}>();"") .Return<DynamicBase>() .Complie<Func<DynamicBase>>(); }} }} var props = typeof({entityClassName}).GetProperties(); for (int i = 0; i < props.Length; i+=1) {{ if(!props[i].PropertyType.IsOnceType()) {{ LinkMapping[props[i].Name] = FastMethodOperator .New .Using(props[i].PropertyType) .Using(""Natasha.Caller"") .MethodBody($@""return {entityClassName}.{{props[i].Name}}.Caller<{{props[i].PropertyType.GetDevelopName()}}>();"") .Return<DynamicBase>() .Complie<Func<DynamicBase>>(); }} }} }}"); body.Append($@" public override DynamicBase Get(string name){{ return LinkMapping[name](); }}"); body.Append($@" public override T Get<T>(string name){{ return {innerClassName}<T>.GetterMapping[name](); }}"); body.Append($@" public override T Get<T>(){{ return {innerClassName}<T>.GetterMapping[_current_name](); }}"); body.Append($@" public override void Set<T>(string name,T value){{ {innerClassName}<T>.SetterMapping[name](value); }}"); body.Append($@" public override void Set<T>(T value){{ {innerClassName}<T>.SetterMapping[_current_name](value); }}"); Type tempClass = builder .Using(type) .Using("System") .Using("System.Collections.Concurrent") .ClassAccess(AccessTypes.Public) .ClassName(className) .Namespace("NatashaDynamicStatic") .Inheritance <DynamicBase>() .ClassBody(body + InnerTemplate.GetStaticInnerString(innerClassName, entityClassName)) .GetType(); return(TypeCreatorMapping[type] = (Func <DynamicBase>)CtorBuilder.NewDelegate(tempClass)); }
public static Func <CallerBase> InitType(Type type) { string className = "NatashaDynamic" + type.GetAvailableName(); string typeName = type.GetDevelopName(); ClassBuilder builder = new ClassBuilder(); StringBuilder body = new StringBuilder(); Dictionary <Type, List <string> > memberCache = new Dictionary <Type, List <string> >(); var fields = type.GetFields(); for (int i = 0; i < fields.Length; i += 1) { if (!memberCache.ContainsKey(fields[i].FieldType)) { memberCache[fields[i].FieldType] = new List <string>(); } memberCache[fields[i].FieldType].Add(fields[i].Name); } var props = type.GetProperties(); for (int i = 0; i < props.Length; i += 1) { if (!memberCache.ContainsKey(props[i].PropertyType)) { memberCache[props[i].PropertyType] = new List <string>(); } memberCache[props[i].PropertyType].Add(props[i].Name); } body.AppendLine("public override T Get<T>(string name){"); body.Append("int typeCode = typeof(T).GetHashCode();"); body.Append("int nameCode = name.GetHashCode();"); int indexType = 0; foreach (var item in memberCache) { if (indexType != 0) { body.Append("else "); } body.Append($"if(typeCode=={item.Key.GetHashCode()}){{"); int indexName = 0; foreach (var name in item.Value) { if (indexName != 0) { body.Append("else "); } body.Append($"if( nameCode == {name.GetHashCode()}){{"); body.Append($"return (T)((object){typeName}.{name});"); body.Append("}"); indexName++; } body.Append("}"); indexType++; } body.Append("return default;}"); body.AppendLine("public override T Get<T>(){"); body.Append("int typeCode = typeof(T).GetHashCode();"); body.Append("int nameCode = _current_name.GetHashCode();"); indexType = 0; foreach (var item in memberCache) { if (indexType != 0) { body.Append("else "); } body.Append($"if(typeCode=={item.Key.GetHashCode()}){{"); int indexName = 0; foreach (var name in item.Value) { if (indexName != 0) { body.Append("else "); } body.Append($"if( nameCode == {name.GetHashCode()}){{"); body.Append($"return (T)((object){typeName}.{name});"); body.Append("}"); indexName++; } body.Append("}"); indexType++; } body.Append("return default;}"); body.AppendLine("public override void Set(string name,object value){"); body.Append("int nameCode = name.GetHashCode();"); foreach (var item in memberCache) { int indexName = 0; foreach (var name in item.Value) { if (indexName != 0) { body.Append("else "); } body.Append($"if( nameCode == {name.GetHashCode()}){{"); body.Append($"{typeName}.{name}=({item.Key.GetDevelopName()})value;"); body.Append("}"); indexName++; } } body.Append("}"); body.AppendLine("public override void Set(object value){"); body.Append("int nameCode = _current_name.GetHashCode();"); foreach (var item in memberCache) { int indexName = 0; foreach (var name in item.Value) { if (indexName != 0) { body.Append("else "); } body.Append($"if( nameCode == {name.GetHashCode()}){{"); body.Append($"{typeName}.{name}=({item.Key.GetDevelopName()})value;"); body.Append("}"); indexName++; } } body.Append("}"); body.AppendLine("public override CallerBase Get(string name){"); body.Append("int nameCode = name.GetHashCode();"); foreach (var item in memberCache) { int indexName = 0; if (!item.Key.IsOnceType()) { foreach (var name in item.Value) { if (indexName != 0) { body.Append("else "); } body.Append($"if( nameCode == {name.GetHashCode()}){{"); body.Append($" return {typeName}.{name}.Caller();"); body.Append("}"); indexName++; } } } body.Append("return null;}"); Type tempClass = builder .Using(type) .Using("System") .Using("NCaller") .Using("System.Collections.Concurrent") .ClassAccess(AccessTypes.Public) .ClassName(className) .Namespace("NCallerDynamic") .Inheritance <CallerBase>() .ClassBody(body) .GetType(); return(TypeCreatorMapping[type] = (Func <CallerBase>)CtorBuilder.NewDelegate(tempClass)); }