//获取变量类型
        string GenerateGetVariableType()
        {
            var templateStr = @"
            case ""{0}"": return typeof({1});";
            var builder     = new StringBuilder();

            //所有类变量
            m_Fields.ForEach((field) => builder.AppendFormat(templateStr, field.Name, ScorpioReflectUtil.GetFullName(field.FieldType)));
            //所有属性
            m_Propertys.ForEach((property) => builder.AppendFormat(templateStr, property.Name, ScorpioReflectUtil.GetFullName(property.PropertyType)));
            //所有的函数
            var methods = new List <string>();

            foreach (var method in m_Methods)
            {
                string name = method.Name;
                if (methods.Contains(name) || method.ReturnType == typeof(void))
                {
                    continue;
                }
                methods.Add(name);
                builder.AppendFormat(templateStr, method.Name, ScorpioReflectUtil.GetFullName(method.ReturnType));
            }
            return(builder.ToString());
        }
        public GenerateScorpioClass(Type type)
        {
            m_Type             = type;
            m_IsStruct         = !type.IsClass;
            m_ScorpioClassName = "ScorpioClass_" + GetClassName(type);
            m_FullName         = ScorpioReflectUtil.GetFullName(m_Type);
            m_AllFields.AddRange(m_Type.GetFields(ScorpioReflectUtil.BindingFlag));
            m_AllEvents.AddRange(m_Type.GetEvents(ScorpioReflectUtil.BindingFlag));
            var propertys = m_Type.GetProperties(ScorpioReflectUtil.BindingFlag);

            foreach (var property in propertys)
            {
                //如果是 get 则参数是0个  set 参数是1个  否则就可能是 [] 的重载
                if ((property.CanRead && property.GetGetMethod().GetParameters().Length == 0) ||
                    (property.CanWrite && property.GetSetMethod().GetParameters().Length == 1))
                {
                    m_AllPropertys.Add(property);
                }
            }
            var methods = (m_Type.IsAbstract && m_Type.IsSealed) ? m_Type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) : m_Type.GetMethods(ScorpioReflectUtil.BindingFlag);

            foreach (var method in methods)
            {
                //屏蔽掉 模版函数 模板函数只能使用反射
                if (!ScorpioReflectUtil.CheckGenericMethod(method))
                {
                    continue;
                }
                m_AllMethods.Add(method);
            }
        }
        //运算符重载函数 + - * / [] 等
        private string GetSpeciaMethodExecute(MethodInfo method, string variable, ParameterInfo[] pars)
        {
            if (!method.IsSpecialName)
            {
                return("");
            }
            string name = method.Name;

            if (Operators.ContainsKey(name))
            {
                return(string.Format("return {0} {1} {2};", GetScorpioMethodArgs(pars, 0), Operators[name], GetScorpioMethodArgs(pars, 1)));
            }
            //重载 =
            if (name == "op_Implicit")
            {
                return(string.Format("return ({0})({1});", ScorpioReflectUtil.GetFullName(method.ReturnType), GetScorpioMethodArgs(pars, 0)));
                //如果 get_Item 参数是一个 就是 [] 的重载
            }
            else if (name == "get_Item" && method.GetParameters().Length == 1)
            {
                return(string.Format("return {0}[{1}];", variable, GetScorpioMethodArgs(pars, 0)));
                //如果 set_Item 参数是两个 就是 [] 的重载
            }
            else if (name == "set_Item" && method.GetParameters().Length == 2)
            {
                return(string.Format("{0}[{1}] = {2}; return null;", variable, GetScorpioMethodArgs(pars, 0), GetScorpioMethodArgs(pars, 1)));
            }
            return("");
        }
        string CreateDelegate(int buildType)
        {
            var builder = new StringBuilder();

            foreach (var type in m_Delegates)
            {
                var fullName     = ScorpioReflectUtil.GetFullName(type);
                var InvokeMethod = type.GetMethod("Invoke");
                var parameters   = InvokeMethod.GetParameters();
                var pars         = "";
                for (int i = 0; i < parameters.Length; ++i)
                {
                    if (i != 0)
                    {
                        pars += ",";
                    }
                    pars += $"arg{i}";
                }
                var invoke = parameters.Length == 0 ? $"scriptObject.call(ScriptValue.Null)" : $"scriptObject.call(ScriptValue.Null,{pars})";
                var call   = ScorpioReflectUtil.ReturnString(invoke, InvokeMethod.ReturnType);
                var func   = $"return new {fullName}( ({pars}) => {{ {call}; }} );";
                if (buildType == 0)
                {
                    builder.Append($@"
        delegates[typeof({fullName})] = (scriptObject) => {{ {func} }};");
                }
                else
                {
                    builder.Append($@"
        if (delegateType == typeof({fullName}))
            {func}");
                }
            }
            return(builder.ToString());
        }
예제 #5
0
 public GenerateScorpioInterface(Type type)
 {
     m_Type           = type;
     FullName         = ScorpioReflectUtil.GetFullName(m_Type);
     ScorpioClassName = "ScorpioInterface_" + ScorpioReflectUtil.GetGenerateClassName(type);
     Methods          = new List <MethodInfo>(type.GetMethods());
 }
예제 #6
0
        string GenerateMethods()
        {
            var builder = new StringBuilder();

            foreach (var method in Methods)
            {
                var parameters    = method.GetParameters();
                var parameterStrs = "";
                var parameterArgs = "";
                for (int i = 0; i < parameters.Length; ++i)
                {
                    if (i != 0)
                    {
                        parameterStrs += ", ";
                    }
                    parameterStrs += $"{parameters[i].ParameterType.GetFullName()} {parameters[i].Name}";
                    parameterArgs += $", {parameters[i].Name}";
                }
                var call = ScorpioReflectUtil.ReturnString($@"__Call(""{method.Name}""{parameterArgs})", method.ReturnType);
                builder.Append($@"
    public {method.ReturnType.GetFullName()} {method.Name}({parameterStrs}) {{
        {call};
    }}");
            }
            return(builder.ToString());
        }
예제 #7
0
        public GenerateScorpioClass(Type type)
        {
            m_Type             = type;
            m_ScorpioClassName = "ScorpioClass_" + GetClassName(type);
            m_FullName         = ScorpioReflectUtil.GetFullName(m_Type);
            m_AllFields.AddRange(m_Type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy));
            m_AllEvents.AddRange(m_Type.GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy));
            m_AllPropertys.AddRange(m_Type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy));
            var methods = (m_Type.IsAbstract && m_Type.IsSealed) ? m_Type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) : m_Type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);

            foreach (var method in methods)
            {
                string name = method.Name;
                //屏蔽掉 =重载函数 IsSpecialName 表示为特殊函数 运算符重载 这个值会为true
                if (method.IsSpecialName && name == "op_Implicit")
                {
                    continue;
                }
                //屏蔽掉 模版函数
                if (method.IsGenericMethod)
                {
                    continue;
                }
                //屏蔽掉 带有 ref 和 out 关键字参数的函数
                if (ScorpioReflectUtil.IsRetvalOrOut(method))
                {
                    continue;
                }
                m_AllMethods.Add(method);
            }
        }
 //根据MethodInfo生成一个ScorpioMethodInfo对象
 private string GetScorpioMethod(MethodBase method)
 {
     if (!method.IsGenericMethod || !method.ContainsGenericParameters)
     {
         string name          = method.Name;
         string isStatic      = method.IsStatic ? "true" : "false";
         string parameterType = "new Type[] {";
         string param         = "false";
         string paramType     = "null";
         var    pars          = method.GetParameters();
         bool   first         = true;
         foreach (var par in pars)
         {
             if (first)
             {
                 first = false;
             }
             else
             {
                 parameterType += ",";
             }
             parameterType += "typeof(" + ScorpioReflectUtil.GetFullName(par.ParameterType) + ")";
             if (Util.IsParamArray(par))
             {
                 param     = "true";
                 paramType = "typeof(" + ScorpioReflectUtil.GetFullName(par.ParameterType.GetElementType()) + ")";
             }
         }
         parameterType += "}";
         return(string.Format(@"new ScorpioMethodInfo(""{0}"", {1}, {2}, {3}, {4}, ""{5}"")", name, isStatic, parameterType, param, paramType, GetScorpioMethodParameterTypes(method)));
     }
     return("");
 }
예제 #9
0
        private string GetScorpioMethodCall(ParameterInfo[] pars)
        {
            var builder = new StringBuilder();

            for (int i = 0; i < pars.Length; ++i)
            {
                if (i != 0)
                {
                    builder.Append(", ");
                }
                var par = pars[i];
                if (par.IsOut)
                {
                    builder.Append($"out retval{i}");
                }
                else if (par.ParameterType.IsByRef)
                {
                    builder.Append($"ref retval{i}");
                }
                else
                {
                    var typeName = ScorpioReflectUtil.GetFullName(par.ParameterType);
                    builder.Append($"({typeName})args[{i}]");
                }
            }
            return(builder.ToString());
        }
 public int Compare(Type x, Type y)
 {
     if (x == null || y == null)
     {
         return(0);
     }
     return(ScorpioReflectUtil.GetFullName(x).CompareTo(ScorpioReflectUtil.GetFullName(y)));
 }
        public string Generate()
        {
            ScorpioReflectUtil.SortType(m_Delegates);
            string str = Template;

            str = str.Replace("__Namespace", string.IsNullOrEmpty(Namespace) ? "ScorpioDelegate" : Namespace);
            str = str.Replace("__ClassName", string.IsNullOrEmpty(ClassName) ? "ScorpioDelegateFactory" : ClassName);
            str = str.Replace("__CreateDelegate", CreateDelegate());
            return(str);
        }
예제 #12
0
        string GenerateGetVariableType()
        {
            string        templateStr = @"        if (name == ""{0}"") return typeof({1});";
            StringBuilder builder     = new StringBuilder();
            bool          first       = true;

            //所有类变量
            foreach (var field in m_Fields)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.AppendLine();
                }
                builder.AppendFormat(templateStr, field.Name, ScorpioReflectUtil.GetFullName(field.FieldType));
            }
            //所有属性
            foreach (var property in m_Propertys)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.AppendLine();
                }
                builder.AppendFormat(templateStr, property.Name, ScorpioReflectUtil.GetFullName(property.PropertyType));
            }
            //所有的函数
            List <string> methods = new List <string>();

            foreach (var method in m_Methods)
            {
                string name = method.Name;
                if (methods.Contains(name) || method.ReturnType == typeof(void))
                {
                    continue;
                }
                methods.Add(name);
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.AppendLine();
                }
                builder.AppendFormat(templateStr, method.Name, ScorpioReflectUtil.GetFullName(method.ReturnType));
            }
            return(builder.ToString());
        }
        public string CreateObject()
        {
            var builder = new StringBuilder();

            foreach (var type in m_Types)
            {
                builder.AppendFormat(@"
            script.SetGlobal(""{0}"", ScriptValue.CreateObject(typeof({0})));", ScorpioReflectUtil.GetFullName(type));
            }
            return(builder.ToString());
        }
        private string GetScorpioMethodParameterTypes(MethodBase method)
        {
            string parameterTypes = "";
            var    pars           = method.GetParameters();

            foreach (var par in pars)
            {
                parameterTypes += ScorpioReflectUtil.GetFullName(par.ParameterType) + "+";
            }
            return(parameterTypes);
        }
 public void AddType(Type type)
 {
     if (type == null || !TYPE_DELEGATE.IsAssignableFrom(type) ||
         // MulticastDelegate 是 event
         type == typeof(MulticastDelegate) || string.IsNullOrWhiteSpace(ScorpioReflectUtil.GetFullName(type)) ||
         m_Delegates.Contains(type))
     {
         return;
     }
     m_Delegates.Add(type);
 }
        string DelegateList()
        {
            var builder = new StringBuilder();

            foreach (var type in m_Delegates)
            {
                var fullName = ScorpioReflectUtil.GetFullName(type);
                builder.Append($@"
        script.SetGlobal(""{fullName}"", ScriptValue.CreateValue(typeof({fullName})));");
            }
            return(builder.ToString());
        }
        private string GetScorpioMethodCall(MethodBase method)
        {
            var call = "";
            var pars = method.GetParameters();

            for (int i = 0; i < pars.Length; ++i)
            {
                if (i != 0)
                {
                    call += ",";
                }
                call += "(" + ScorpioReflectUtil.GetFullName(pars[i].ParameterType) + ")args[" + i + "]";
            }
            call += "";
            return(call);
        }
        //生成SetValue函数
        private string GenerateSetValue()
        {
            var builder       = new StringBuilder();
            var reflectFormat = @"
            case ""{0}"": {{ {1}.SetValue(obj, Util.ChangeType(value, typeof({2}))); return; }}";
            var normalFormat  = @"
            case ""{0}"": {{ {1} = ({2})(Util.ChangeType(value, typeof({2}))); return; }}";

            //类变量
            foreach (var field in m_Fields)
            {
                if (field.IsInitOnly /*readonly 属性*/ || field.IsLiteral /*const 属性*/)
                {
                    continue;
                }
                var fieldFullName = ScorpioReflectUtil.GetFullName(field.FieldType);
                if (IsStruct && !field.IsStatic)
                {
                    builder.AppendFormat(reflectFormat, field.Name, $"_field_{field.Name}", fieldFullName);
                }
                else
                {
                    builder.AppendFormat(normalFormat, field.Name, GetScorpioVariable(field.IsStatic, field.Name), fieldFullName);
                }
            }
            //类属性
            foreach (var property in m_Propertys)
            {
                MethodInfo setMethod;
                if (!property.CanWrite || (setMethod = property.GetSetMethod(false)) == null)
                {
                    continue;
                }
                var propertyFullName = ScorpioReflectUtil.GetFullName(property.PropertyType);
                if (IsStruct && !setMethod.IsStatic)
                {
                    builder.AppendFormat(reflectFormat, property.Name, $"_property_{property.Name}", propertyFullName);
                }
                else
                {
                    builder.AppendFormat(normalFormat, property.Name, GetScorpioVariable(setMethod.IsStatic, property.Name), propertyFullName);
                }
            }
            return(builder.ToString());
        }
        //根据MethodInfo生成一个ScorpioMethodInfo对象
        private string GetScorpioMethod(MethodBase method, int index)
        {
            var  isStatic      = method.IsStatic ? "true" : "false";
            var  paramType     = "null";
            var  parameterType = new StringBuilder("new Type[]{");
            var  refOut        = new StringBuilder("new bool[]{");
            var  pars          = method.GetParameters();
            bool first         = true;

            foreach (var par in pars)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    parameterType.Append(","); refOut.Append(",");
                }
                if (Util.IsRetvalOrOut(par))
                {
                    parameterType.Append($"typeof({ScorpioReflectUtil.GetFullName(par.ParameterType.GetElementType())})");
                    refOut.Append("true");
                }
                else
                {
                    refOut.Append("false");
                    parameterType.Append($"typeof({ScorpioReflectUtil.GetFullName(par.ParameterType)})");
                    if (Util.IsParams(par))
                    {
                        paramType = $"typeof({ScorpioReflectUtil.GetFullName(par.ParameterType.GetElementType())})";
                    }
                }
            }
            parameterType.Append("}");
            refOut.Append("}");
            return(string.Format(@"new ScorpioFastReflectMethodInfo({0}, {1}, {2}, {3}, {4})", isStatic, parameterType.ToString(), refOut.ToString(), paramType, index));
        }
 private string GetScorpioMethodArgs(ParameterInfo[] pars, int index)
 {
     return("(" + ScorpioReflectUtil.GetFullName(pars[index].ParameterType) + ")args[" + index + "]");
 }
예제 #21
0
        public string CreateObject()
        {
            StringBuilder builder = new StringBuilder();
            bool          first   = true;

            foreach (var type in m_Types)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.AppendLine();
                }
                builder.AppendFormat("            script.SetObject(\"{0}\", script.CreateObject(typeof({0})));", ScorpioReflectUtil.GetFullName(type));
            }
            return(builder.ToString());
        }
        //生成SetValue函数
        private string GenerateSetValue()
        {
            string        fieldStr = @"        if (name == ""{0}"") {{ {1} = ({2})(Util.ChangeType(m_Script, value, typeof({2}))); return; }}";
            StringBuilder builder  = new StringBuilder();
            bool          first    = true;

            //类变量
            foreach (var field in m_Fields)
            {
                if (field.IsInitOnly /*readonly 属性*/ || field.IsLiteral /*const 属性*/)
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.AppendLine();
                }
                builder.AppendFormat(fieldStr, field.Name, GetScorpioVariable(field.IsStatic, field.Name), ScorpioReflectUtil.GetFullName(field.FieldType));
            }
            //所有属性
            foreach (var property in m_Propertys)
            {
                if (property.CanRead && property.GetSetMethod(false) != null)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        builder.AppendLine();
                    }
                    builder.AppendFormat(fieldStr, property.Name, GetScorpioVariable(property.GetSetMethod(false).IsStatic, property.Name), ScorpioReflectUtil.GetFullName(property.PropertyType));
                }
            }
            return(builder.ToString());
        }
예제 #23
0
 void Init()
 {
     (m_Fields = new List <FieldInfo>(AllFields.Where(_ => Check(_)))).SortField();
     (m_Events = new List <EventInfo>(AllEvents.Where(_ => Check(_)))).SortEvent();
     (m_Propertys = new List <PropertyInfo>(AllPropertys.Where(_ => Check(_)))).SortProperty();
     m_Methods.Clear();
     foreach (var method in AllMethods)
     {
         if (!Check(method))
         {
             continue;
         }
         var valid        = true;
         var methodHandle = method.MethodHandle;
         //判断函数是不是 event add remove 函数
         foreach (var eventInfo in AllEvents)
         {
             if (eventInfo.GetAddMethod().MethodHandle == methodHandle || eventInfo.GetRemoveMethod().MethodHandle == methodHandle)
             {
                 if (!Check(eventInfo))
                 {
                     valid = false;
                 }
                 break;
             }
         }
         if (valid)
         {
             //判断函数是不是 get set 函数
             foreach (var propertyInfo in AllPropertys)
             {
                 //如果是struct 并且是 set 属性则屏蔽,强转结构体的set会报错
                 if (IsStruct && propertyInfo.GetSetMethod()?.MethodHandle == methodHandle)
                 {
                     valid = false;
                     break;
                 }
                 else if (propertyInfo.GetGetMethod()?.MethodHandle == methodHandle || propertyInfo.GetSetMethod()?.MethodHandle == methodHandle)
                 {
                     if (!Check(propertyInfo))
                     {
                         valid = false;
                     }
                     break;
                 }
             }
         }
         if (valid)
         {
             m_Methods.Add(method);
         }
     }
     m_Methods.SortMethod();
     m_ExtensionMethods.Clear();
     foreach (var extensionInfo in m_Extensions)
     {
         var nameSpace = extensionInfo.Namespace;
         var methods   = extensionInfo.GetMethods(Script.BindingFlag);
         foreach (var methodInfo in methods)
         {
             //非扩展函数
             if (!Util.IsExtensionMethod(methodInfo) || !Check(methodInfo))
             {
                 continue;
             }
             var paramterType = methodInfo.GetParameters()[0].ParameterType;
             //判断是模板函数
             if (paramterType.IsGenericParameter && paramterType.BaseType != null && paramterType.BaseType.IsAssignableFrom(m_Type))
             {
                 m_ExtensionMethods.Add(methodInfo);
             }
             else if (ScorpioReflectUtil.CheckGenericMethod(methodInfo) && paramterType.IsAssignableFrom(m_Type))
             {
                 m_ExtensionMethods.Add(methodInfo);
             }
             else
             {
                 continue;
             }
             if (!string.IsNullOrWhiteSpace(nameSpace))
             {
                 m_ExtensionUsing.Add(nameSpace);
             }
         }
     }
     m_ExtensionMethods.SortMethod();
     m_Methods.ForEach(_ => m_MethodNames.Add(_.Name));
     m_ExtensionMethods.ForEach(_ => m_MethodNames.Add(_.Name));
 }
 void Init()
 {
     {
         m_Fields.Clear();
         foreach (var field in m_AllFields)
         {
             if (m_ClassFilter != null && !m_ClassFilter.Check(this, m_Type, field))
             {
                 continue;
             }
             m_Fields.Add(field);
         }
         m_Fields.Sort(new ComparerFieldInfo());
     }
     {
         m_Events.Clear();
         foreach (var eve in m_AllEvents)
         {
             if (m_ClassFilter != null && !m_ClassFilter.Check(this, m_Type, eve))
             {
                 continue;
             }
             m_Events.Add(eve);
         }
         m_Events.Sort(new ComparerEventInfo());
     }
     {
         m_Propertys.Clear();
         foreach (var property in m_AllPropertys)
         {
             if (m_ClassFilter != null && !m_ClassFilter.Check(this, m_Type, property))
             {
                 continue;
             }
             m_Propertys.Add(property);
         }
         m_Propertys.Sort(new ComparerPropertyInfo());
     }
     {
         m_Methods.Clear();
         foreach (var method in m_AllMethods)
         {
             if (m_ClassFilter != null && !m_ClassFilter.Check(this, m_Type, method))
             {
                 continue;
             }
             bool check        = true;
             var  methodHandle = method.MethodHandle;
             //判断函数是不是 event add remove 函数
             foreach (var eve in m_AllEvents)
             {
                 if (eve.GetAddMethod().MethodHandle == methodHandle || eve.GetRemoveMethod().MethodHandle == methodHandle)
                 {
                     if (m_ClassFilter != null && !m_ClassFilter.Check(this, m_Type, eve))
                     {
                         check = false;
                     }
                     break;
                 }
             }
             if (check)
             {
                 //判断函数是不是 get set 函数
                 foreach (var property in m_AllPropertys)
                 {
                     if (m_IsStruct && property.GetSetMethod()?.MethodHandle == methodHandle)
                     {
                         check = false;
                         break;
                     }
                     else if (property.GetGetMethod()?.MethodHandle == methodHandle || property.GetSetMethod()?.MethodHandle == methodHandle)
                     {
                         if (m_ClassFilter != null && !m_ClassFilter.Check(this, m_Type, property))
                         {
                             check = false;
                         }
                         break;
                     }
                 }
             }
             if (check)
             {
                 m_Methods.Add(method);
             }
         }
         m_Methods.Sort(new ComparerMethodInfo());
     }
     {
         m_ExtensionMethods.Clear();
         foreach (var exten in m_Extensions)
         {
             var nameSpace = exten.Namespace;
             var methods   = exten.GetMethods(Script.BindingFlag);
             foreach (var method in methods)
             {
                 if (!Util.IsExtensionMethod(method))
                 {
                     continue;
                 }
                 var paramterType = method.GetParameters()[0].ParameterType;
                 //判断是模板函数
                 if (paramterType.IsGenericParameter && paramterType.BaseType != null && (m_Type == paramterType.BaseType || m_Type.IsSubclassOf(paramterType.BaseType)))
                 {
                     m_ExtensionMethods.Add(method);
                 }
                 else if (ScorpioReflectUtil.CheckGenericMethod(method) && (m_Type == paramterType || m_Type.IsSubclassOf(paramterType)))
                 {
                     m_ExtensionMethods.Add(method);
                 }
                 else
                 {
                     continue;
                 }
                 if (!string.IsNullOrWhiteSpace(nameSpace) && !m_ExtensionUsing.Contains(nameSpace))
                 {
                     m_ExtensionUsing.Add(nameSpace);
                 }
             }
         }
         m_ExtensionUsing.Sort();
         m_ExtensionMethods.Sort(new ComparerMethodInfo());
     }
     {
         m_MethodNames.Clear();
         foreach (var method in m_Methods)
         {
             string name = method.Name;
             if (m_MethodNames.Contains(name))
             {
                 continue;
             }
             m_MethodNames.Add(name);
         }
         foreach (var method in m_ExtensionMethods)
         {
             string name = method.Name;
             if (m_MethodNames.Contains(name))
             {
                 continue;
             }
             m_MethodNames.Add(name);
         }
         m_MethodNames.Sort();
     }
 }
예제 #25
0
        string CreateDelegate(int buildType)
        {
            var builder = new StringBuilder();

            foreach (var type in m_Delegates)
            {
                var fullName     = ScorpioReflectUtil.GetFullName(type);
                var InvokeMethod = type.GetMethod("Invoke");
                var parameters   = InvokeMethod.GetParameters();
                var pars         = "";
                for (int i = 0; i < parameters.Length; ++i)
                {
                    if (i != 0)
                    {
                        pars += ",";
                    }
                    pars += $"arg{i}";
                }
                var invoke         = parameters.Length == 0 ? $"scriptObject.call(ScriptValue.Null)" : $"scriptObject.call(ScriptValue.Null,{pars})";
                var returnType     = InvokeMethod.ReturnType;
                var returnFullName = ScorpioReflectUtil.GetFullName(returnType);
                var call           = "";
                if (returnType == typeof(void))
                {
                    call = $"{invoke}";
                }
                else if (returnType == typeof(ScriptValue))
                {
                    call = $"return {invoke}";
                }
                else if (returnType == typeof(bool))
                {
                    call = $"return {invoke}.IsTrue";
                }
                else if (returnType == typeof(string))
                {
                    call = $"return {invoke}.ToString()";
                }
                else if (returnType == typeof(sbyte) || returnType == typeof(byte) ||
                         returnType == typeof(short) || returnType == typeof(ushort) ||
                         returnType == typeof(int) || returnType == typeof(uint) ||
                         returnType == typeof(long) || returnType == typeof(ulong) ||
                         returnType == typeof(float) || returnType == typeof(double) || returnType == typeof(decimal))
                {
                    call = $"return ({returnFullName})Convert.ChangeType({invoke}.Value, typeof({returnFullName}))";
                }
                else
                {
                    call = $"return ({returnFullName}){invoke}.Value";
                }
                var func = $"return new {fullName}( ({pars}) => {{ {call}; }} );";
                if (buildType == 0)
                {
                    builder.Append($@"
        delegates[typeof({fullName})] = (scriptObject) => {{ {func} }};");
                }
                else
                {
                    builder.Append($@"
        if (delegateType == typeof({fullName}))
            {func}");
                }
            }
            return(builder.ToString());
        }
        public string CreateDelegate()
        {
            StringBuilder builder = new StringBuilder();
            bool          first   = true;

            foreach (var type in m_Delegates)
            {
                if (!TYPE_DELEGATE.IsAssignableFrom(type))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.AppendLine();
                }
                builder.AppendLine("            if (type == typeof(__Name))".Replace("__Name", ScorpioReflectUtil.GetFullName(type)));
                builder.Append("                return new __Name(".Replace("__Name", ScorpioReflectUtil.GetFullName(type)));
                var    InvokeMethod = type.GetMethod("Invoke");
                var    parameters   = InvokeMethod.GetParameters();
                string pars         = "";
                for (int i = 0; i < parameters.Length; ++i)
                {
                    if (i != 0)
                    {
                        pars += ",";
                    }
                    pars += ("arg" + i);
                }
                builder.Append("(" + pars + ") => { ");
                var returnType = InvokeMethod.ReturnType;
                if (returnType == typeof(void))
                {
                    builder.Append("func.call(" + pars + ");");
                }
                else if (returnType == typeof(bool))
                {
                    builder.Append("return script.CreateObject(func.call(" + pars + ")).LogicOperation();");
                }
                else if (returnType == typeof(string))
                {
                    builder.Append("return script.CreateObject(func.call(" + pars + ")).ToString();");
                }
                else if (returnType == typeof(sbyte) || returnType == typeof(byte) ||
                         returnType == typeof(short) || returnType == typeof(ushort) ||
                         returnType == typeof(int) || returnType == typeof(uint) ||
                         returnType == typeof(long) || returnType == typeof(ulong) ||
                         returnType == typeof(float) || returnType == typeof(double) || returnType == typeof(decimal))
                {
                    string str = "return (__Type)Convert.ChangeType(script.CreateObject(func.call(" + pars + ")).ObjectValue, typeof(__Type));";
                    str = str.Replace("__Type", ScorpioReflectUtil.GetFullName(returnType));
                    builder.Append(str);
                }
                else if (typeof(ScriptObject).IsAssignableFrom(returnType))
                {
                    builder.Append("return script.CreateObject(func.call(" + pars + "));");
                }
                else
                {
                    string str = "return (__Type)script.CreateObject(func.call(" + pars + ")).ObjectValue;";
                    str = str.Replace("__Type", ScorpioReflectUtil.GetFullName(returnType));
                    builder.Append(str);
                }
                builder.Append(" });");
            }
            return(builder.ToString());
        }
예제 #27
0
        //生成SetValue函数
        private string GenerateSetValue()
        {
            if (m_IsStruct)
            {
                return("");
            }
            var fieldStr = @"
            case ""{0}"": {{ {1} = ({2})(Util.ChangeType(value, typeof({2}))); return; }}";
            var builder  = new StringBuilder();

            //类变量
            foreach (var field in m_Fields)
            {
                if (field.IsInitOnly /*readonly 属性*/ || field.IsLiteral /*const 属性*/)
                {
                    continue;
                }
                builder.AppendFormat(fieldStr, field.Name, GetScorpioVariable(field.IsStatic, field.Name), ScorpioReflectUtil.GetFullName(field.FieldType));
            }
            //所有属性
            foreach (var property in m_Propertys)
            {
                if (property.CanWrite && property.GetSetMethod(false) != null)
                {
                    builder.AppendFormat(fieldStr, property.Name, GetScorpioVariable(property.GetSetMethod(false).IsStatic, property.Name), ScorpioReflectUtil.GetFullName(property.PropertyType));
                }
            }
            return(builder.ToString());
        }
예제 #28
0
        private string GetExecuteMethod(ParameterInfo[] pars, bool hasReturn, string call)
        {
            var callBuilder = new StringBuilder();
            var hasRefOut   = false;

            for (var j = 0; j < pars.Length; ++j)
            {
                if (Util.IsRetvalOrOut(pars[j]))
                {
                    hasRefOut = true;
                    var typeName = ScorpioReflectUtil.GetFullName(pars[j].ParameterType.GetElementType());
                    callBuilder.Append($@"
                    var retval{j} = args[{j}] == null ? default({typeName}) : ({typeName})args[{j}]; ");
                }
            }
            if (hasRefOut)
            {
                if (hasReturn)
                {
                    callBuilder.Append($@"
                    var __Result = {call};");
                }
                else
                {
                    callBuilder.Append($@"
                    {call};");
                }
                for (var j = 0; j < pars.Length; ++j)
                {
                    if (Util.IsRetvalOrOut(pars[j]))
                    {
                        callBuilder.Append($@"
                    args[{j}] = retval{j};");
                    }
                }
                if (hasReturn)
                {
                    callBuilder.Append(@"
                    return __Result;
               ");
                }
                else
                {
                    callBuilder.Append(@"
                    return null;
               ");
                }
                return(callBuilder.ToString());
            }
            else
            {
                if (hasReturn)
                {
                    callBuilder.Append($"return {call};");
                }
                else
                {
                    callBuilder.Append($"{call}; return null;");
                }
                return(callBuilder.ToString());
            }
        }
예제 #29
0
 public string Generate(int buildType)
 {
     ScorpioReflectUtil.SortType(m_Delegates);
     return((buildType == 0 ? Template : TemplateIf).Replace("__DelegateList", DelegateList()).Replace("__CreateDelegate", CreateDelegate(buildType)));
 }