public static Delegate NewDelegate(Type type) { return(MethodBuilder.NewMethod .Body($@"return new {TypeReverser.Get(type)}();") .Return(type) .Create()); }
public static Func <T> NewDelegate <T>() { return(MethodBuilder.NewMethod .Using <T>() .Body($@"return new {TypeReverser.Get(typeof(T))}();") .Return <T>() .Create <Func <T> >()); }
public static Func <object> NewDelegate(Type type) { return(MethodBuilder.NewMethod .Using(type) .Body($@"return new {TypeReverser.Get(type)}();") .Return <object>() .Create <Func <object> >()); }
public LINK Inheritance(Type type) { if (type == null) { return(_link); } Using(type); return(Inheritance(TypeReverser.Get(type))); }
public LINK Field(string level, Type type, string name) { Using(type); if (!_fieldsSet.Contains(name)) { _fields.Append($"{level} {TypeReverser.Get(type)} {name};"); } return(_link); }
/// <summary> /// 获取动态方法体 /// </summary> /// <returns></returns> public string GetScript(bool isStatic = true) { if (_info != null) { MethodTemplate template = new MethodTemplate(_info); return(template.Body(_text).Builder()); } else { StringBuilder sb = new StringBuilder(); sb.Append($"public "); if (isStatic) { sb.Append($"static "); } if (_return_type == null) { sb.Append("void"); } else { sb.Append(_return_type.Name); } if (_method == null) { sb.Append($" {_method_name}"); } else { sb.Append(" " + _method); } sb.Append("("); if (_parameters.Count > 0) { sb.Append($"{TypeReverser.Get(_parameters[0].Key)} {_parameters[0].Value}"); for (int i = 1; i < _parameters.Count; i++) { sb.Append($",{TypeReverser.Get(_parameters[i].Key)} {_parameters[i].Value}"); } } sb.Append("){"); sb.Append(_text); sb.Append("}"); return(sb.ToString()); } }
public static void ComplieToRemote(Type type) { string className = TypeReverser.Get(type); if (!RemoteReader._func_mapping.ContainsKey(className)) { RemoteReader._func_mapping[className] = new ConcurrentDictionary <string, Func <RemoteParameters, string> >(); MethodInfo[] infos = type.GetMethods(); foreach (var item in infos) { if (item.ReturnType == typeof(void)) { continue; } var parameters = item.GetParameters(); StringBuilder sb = new StringBuilder(); StringBuilder call = new StringBuilder($"{item.Name}("); if (parameters != null) { if (parameters.Length > 0) { call.Append(parameters[0].Name); sb.Append($"{TypeReverser.Get(parameters[0].ParameterType)} {parameters[0].Name} = {Deserialization}<{TypeReverser.Get(parameters[0].ParameterType)}>(parameters[\"{parameters[0].Name}\"]);"); for (int i = 1; i < parameters.Length; i++) { call.Append($",{parameters[i].Name}"); sb.Append($"{TypeReverser.Get(parameters[i].ParameterType)} {parameters[i].Name} = {Deserialization}<{TypeReverser.Get(parameters[i].ParameterType)}>(parameters[\"{parameters[i].Name}\"]);"); } } } RemoteReader._func_mapping[className][item.Name] = MethodBuilder .NewMethod .Using(type) .Using(typeof(JsonConvert)) .Param <RemoteParameters>("parameters") .Body( $@"{sb}{className} instance = new {className}();return {Serialization}(instance.{call}));" ).Return <string>().Create <Func <RemoteParameters, string> >(); } } }
public string Create(string content) { if (_info.IsPublic) { _script.Append("public "); } else if (_info.IsPrivate) { _script.Append("private "); } if (_prefix != null) { _script.Append(_prefix); _script.Append(' '); } _script.Append(TypeReverser.Get(_info.ReturnType)); _script.Append(' '); _script.Append(_info.Name); _script.Append("("); var parameters = _info.GetParameters(); if (parameters != null) { if (parameters.Length > 0) { _script.Append(ParameterInfoReverser.Get(parameters[0])); _script.Append(' '); _script.Append(parameters[0].Name); for (int i = 1; i < parameters.Length; i++) { _script.Append(','); _script.Append(ParameterInfoReverser.Get(parameters[i])); _script.Append(' '); _script.Append(parameters[i].Name); } } } _script.Append("){"); _script.Append(content); _script.Append("}"); return(_script.ToString()); }
public static Func <T> NewDelegate <T>(Type type = null) { var builder = MethodBuilder.NewMethod; if (type == null) { //直接使用T的类型作为初始化类型 type = typeof(T); } else { //T为object,那么自动加载type的命名空间 builder.Using(type); } return(builder .Using <T>() .Body($@"return new {TypeReverser.Get(type)}();") .Return <T>() .Create <Func <T> >()); }