コード例 #1
0
        private void CollectMethods(IEnumerable <MethodInfo> methods, bool asExtensionAnyway)
        {
            foreach (var method in methods)
            {
                if (BindingManager.IsGenericMethod(method))
                {
                    bindingManager.Info("skip generic method: {0}", method);
                    continue;
                }

                if (BindingManager.ContainsPointer(method))
                {
                    bindingManager.Info("skip unsafe (pointer) method: {0}", method);
                    continue;
                }

                if (method.IsSpecialName)
                {
                    if (!IsSupportedOperators(method))
                    {
                        bindingManager.Info("skip special method: {0}", method);
                        continue;
                    }
                }

                if (method.IsDefined(typeof(JSOmitAttribute), false))
                {
                    bindingManager.Info("skip omitted method: {0}", method);
                    continue;
                }

                if (method.IsDefined(typeof(ObsoleteAttribute), false))
                {
                    bindingManager.Info("skip obsolete method: {0}", method);
                    continue;
                }

                if (transform.IsMemberBlocked(method.Name))
                {
                    bindingManager.Info("skip blocked method: {0}", method.Name);
                    continue;
                }

                if (transform.Filter(method))
                {
                    bindingManager.Info("skip filtered method: {0}", method.Name);
                    continue;
                }

                if (asExtensionAnyway || BindingManager.IsExtensionMethod(method))
                {
                    var targetType = method.GetParameters()[0].ParameterType;
                    var targetInfo = bindingManager.GetExportedType(targetType);
                    if (targetInfo != null)
                    {
                        targetInfo.AddMethod(method, true);
                        continue;
                    }
                }

                AddMethod(method, false);
            }
        }
コード例 #2
0
        public void AddMethod(MethodInfo methodInfo, bool asExtensionAnyway)
        {
            if (this.transform.IsBlocked(methodInfo))
            {
                bindingManager.Info("skip blocked method: {0}", methodInfo.Name);
                return;
            }

            // if (type.IsConstructedGenericType)
            // {
            //     var gTransform = bindingManager.GetTypeTransform(type.GetGenericTypeDefinition());
            //     if (gTransform != null && gTransform.IsBlocked(methodInfo.??))
            //     {
            //         bindingManager.Info("skip blocked method in generic definition: {0}", methodInfo.Name);
            //         return;
            //     }
            // }

            var isExtension = asExtensionAnyway || BindingManager.IsExtensionMethod(methodInfo);
            var isStatic    = methodInfo.IsStatic && !isExtension;

            if (isStatic && type.IsGenericTypeDefinition)
            {
                bindingManager.Info("skip static method in generic type definition: {0}", methodInfo.Name);
                return;
            }

            var methodCSName = methodInfo.Name;
            var methodJSName = this.bindingManager.GetNamingAttribute(this.transform, methodInfo);

            if (IsOperatorOverloadingEnabled(methodInfo))
            {
                var parameters    = methodInfo.GetParameters();
                var declaringType = methodInfo.DeclaringType;
                OperatorBindingInfo operatorBindingInfo = null;
                switch (methodCSName)
                {
                case "op_LessThan":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, methodCSName, "<", "<", 2);
                        }
                    }
                    break;

                case "op_Addition":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, methodCSName, "+", "+", 2);
                        }
                    }
                    break;

                case "op_Subtraction":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, methodCSName, "-", "-", 2);
                        }
                    }
                    break;

                case "op_Equality":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, methodCSName, "==", "==", 2);
                        }
                    }
                    break;

                case "op_Multiply":
                    if (parameters.Length == 2)
                    {
                        var op0 = bindingManager.GetExportedType(parameters[0].ParameterType);
                        var op1 = bindingManager.GetExportedType(parameters[1].ParameterType);
                        if (op0 != null && op1 != null)
                        {
                            var bindingName = methodCSName + "_" + op0.csBindingName + "_" + op1.csBindingName;
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, bindingName, "*", "*", 2);
                        }
                    }
                    break;

                case "op_Division":
                    if (parameters.Length == 2)
                    {
                        var op0 = bindingManager.GetExportedType(parameters[0].ParameterType);
                        var op1 = bindingManager.GetExportedType(parameters[1].ParameterType);
                        if (op0 != null && op1 != null)
                        {
                            var bindingName = methodCSName + "_" + op0.csBindingName + "_" + op1.csBindingName;
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, bindingName, "/", "/", 2);
                        }
                    }
                    break;

                case "op_UnaryNegation":
                {
                    operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, methodCSName, "neg", "-", 1);
                }
                break;
                }

                if (operatorBindingInfo != null)
                {
                    operators.Add(operatorBindingInfo);
                    CollectDelegate(methodInfo);
                    bindingManager.Info("[AddOperator] {0}.{1}", type, methodInfo);
                    return;
                }

                // fallback to normal method binding
            }

            var group = isStatic ? staticMethods : methods;
            MethodBindingInfo methodBindingInfo;

            if (!group.TryGetValue(methodCSName, out methodBindingInfo))
            {
                methodBindingInfo = new MethodBindingInfo(bindingManager, isStatic, methodCSName, methodJSName);
                group.Add(methodCSName, methodBindingInfo);
            }

            if (!methodBindingInfo.Add(methodInfo, isExtension))
            {
                bindingManager.Info("fail to add method: {0}", methodInfo.Name);
                return;
            }

            CollectDelegate(methodInfo);
            bindingManager.Info("[AddMethod] {0}.{1}", type, methodInfo);
        }
コード例 #3
0
        // 收集所有 字段,属性,方法
        public void Collect()
        {
            this.constructors = new ConstructorBindingInfo(bindingManager, type);

            var bindingFlags = Binding.DynamicType.PublicFlags;
            var fields       = type.GetFields(bindingFlags);

            foreach (var field in fields)
            {
                if (field.IsSpecialName || field.Name.StartsWith("_JSFIX_"))
                {
                    bindingManager.Info("skip special field: {0}", field.Name);
                    continue;
                }

                if (field.IsStatic && type.IsGenericTypeDefinition)
                {
                    bindingManager.Info("skip static field in generic type definition: {0}", field.Name);
                    return;
                }

                if (field.FieldType.IsPointer)
                {
                    bindingManager.Info("skip pointer field: {0}", field.Name);
                    continue;
                }

                if (field.IsDefined(typeof(JSOmitAttribute), false))
                {
                    bindingManager.Info("skip omitted field: {0}", field.Name);
                    continue;
                }

                if (field.IsDefined(typeof(ObsoleteAttribute), false))
                {
                    bindingManager.Info("skip obsolete field: {0}", field.Name);
                    continue;
                }

                if (transform.IsMemberBlocked(field.Name))
                {
                    bindingManager.Info("skip blocked field: {0}", field.Name);
                    continue;
                }

                if (transform.Filter(field))
                {
                    bindingManager.Info("skip filtered field: {0}", field.Name);
                    continue;
                }

                AddField(field);
            }

            var events = type.GetEvents(bindingFlags);

            foreach (var evt in events)
            {
                if (evt.IsSpecialName)
                {
                    bindingManager.Info("skip special event: {0}", evt.Name);
                    continue;
                }

                if ((evt.GetAddMethod(true)?.IsStatic == true || evt.GetRemoveMethod(true)?.IsStatic == true) && type.IsGenericTypeDefinition)
                {
                    bindingManager.Info("skip static event in generic type definition: {0}", evt.Name);
                    return;
                }

                if (evt.EventHandlerType.IsPointer)
                {
                    bindingManager.Info("skip pointer event: {0}", evt.Name);
                    continue;
                }

                if (evt.IsDefined(typeof(ObsoleteAttribute), false))
                {
                    bindingManager.Info("skip obsolete event: {0}", evt.Name);
                    continue;
                }

                if (evt.IsDefined(typeof(JSOmitAttribute), false))
                {
                    bindingManager.Info("skip omitted event: {0}", evt.Name);
                    continue;
                }

                if (transform.IsMemberBlocked(evt.Name))
                {
                    bindingManager.Info("skip blocked event: {0}", evt.Name);
                    continue;
                }

                if (transform.Filter(evt))
                {
                    bindingManager.Info("skip filtered event: {0}", evt.Name);
                    continue;
                }

                AddEvent(evt);
            }

            var properties = type.GetProperties(bindingFlags);

            foreach (var property in properties)
            {
                if (property.IsSpecialName)
                {
                    bindingManager.Info("skip special property: {0}", property.Name);
                    continue;
                }

                if (property.PropertyType.IsPointer)
                {
                    bindingManager.Info("skip pointer property: {0}", property.Name);
                    continue;
                }

                var propInfoGetMethod = property.GetGetMethod(true);
                var propInfoSetMethod = property.GetSetMethod(true);

                if ((propInfoGetMethod?.IsStatic == true || propInfoSetMethod?.IsStatic == true) && type.IsGenericTypeDefinition)
                {
                    bindingManager.Info("skip static property in generic type definition: {0}", property.Name);
                    return;
                }

                if (property.IsDefined(typeof(JSOmitAttribute), false))
                {
                    bindingManager.Info("skip omitted property: {0}", property.Name);
                    continue;
                }

                if (property.IsDefined(typeof(ObsoleteAttribute), false))
                {
                    bindingManager.Info("skip obsolete property: {0}", property.Name);
                    continue;
                }

                if (transform.IsMemberBlocked(property.Name))
                {
                    bindingManager.Info("skip blocked property: {0}", property.Name);
                    continue;
                }

                if (transform.Filter(property))
                {
                    bindingManager.Info("skip filtered property: {0}", property.Name);
                    continue;
                }

                //NOTE: 索引访问
                if (property.Name == "Item")
                {
                    if (property.CanRead && propInfoGetMethod != null && propInfoGetMethod.IsPublic)
                    {
                        if (BindingManager.IsUnsupported(propInfoGetMethod))
                        {
                            bindingManager.Info("skip unsupported get-method: {0}", propInfoGetMethod);
                            continue;
                        }

                        AddMethod(propInfoGetMethod, false);
                    }

                    if (property.CanWrite && propInfoSetMethod != null && propInfoSetMethod.IsPublic)
                    {
                        if (BindingManager.IsUnsupported(propInfoSetMethod))
                        {
                            bindingManager.Info("skip unsupported set-method: {0}", propInfoSetMethod);
                            continue;
                        }

                        AddMethod(propInfoSetMethod, false);
                    }

                    // bindingManager.Info("skip indexer property: {0}", property.Name);
                    continue;
                }

                AddProperty(property);
            }

            if (!type.IsAbstract)
            {
                var constructors = type.GetConstructors();
                foreach (var constructor in constructors)
                {
                    if (constructor.IsDefined(typeof(JSOmitAttribute), false))
                    {
                        bindingManager.Info("skip omitted constructor: {0}", constructor);
                        continue;
                    }

                    if (constructor.IsDefined(typeof(ObsoleteAttribute), false))
                    {
                        bindingManager.Info("skip obsolete constructor: {0}", constructor);
                        continue;
                    }

                    if (BindingManager.ContainsPointer(constructor))
                    {
                        bindingManager.Info("skip pointer-param constructor: {0}", constructor);
                        continue;
                    }

                    if (BindingManager.ContainsByRefParameters(constructor))
                    {
                        bindingManager.Info("skip byref-param constructor: {0}", constructor);
                        continue;
                    }

                    if (transform.Filter(constructor))
                    {
                        bindingManager.Info("skip filtered constructor: {0}", constructor.Name);
                        continue;
                    }

                    AddConstructor(constructor);
                }
            }

            CollectMethods(type.GetMethods(bindingFlags), false);
            CollectMethods(transform.extensionMethods, true);
            CollectMethods(transform.staticMethods, false);
        }
コード例 #4
0
 public virtual void OnCleanup(BindingManager bindingManager)
 {
 }
コード例 #5
0
        public TSTypeNaming(BindingManager bindingManager, Type type, TypeTransform typeTransform)
        {
            this.type = type;

            var naming          = typeTransform?.GetTypeNaming() ?? type.Name;
            var indexOfTypeName = naming.LastIndexOf('.');

            if (indexOfTypeName >= 0)
            {
                // 指定的命名中已经携带了"."
                var indexOfInnerTypeName = naming.IndexOf('+');
                if (indexOfInnerTypeName >= 0)
                {
                    this.jsModule = naming.Substring(0, indexOfInnerTypeName);
                    var rightName = naming.Substring(indexOfInnerTypeName + 1);
                    var lastIndexOfInnerTypeName = rightName.LastIndexOf('+');
                    if (lastIndexOfInnerTypeName >= 0)
                    {
                        this.jsNamespace = rightName.Substring(0, lastIndexOfInnerTypeName);
                        this.jsName      = rightName.Substring(lastIndexOfInnerTypeName + 1);
                    }
                    else
                    {
                        this.jsNamespace = "";
                        this.jsName      = rightName;
                    }
                }
                else
                {
                    this.jsModule    = naming.Substring(0, indexOfTypeName);
                    this.jsNamespace = "";
                    this.jsName      = naming.Substring(indexOfTypeName + 1);
                }

                var gArgIndex = this.jsName.IndexOf("<");
                if (gArgIndex < 0)
                {
                    this.jsPureName = this.jsName;
                }
                else
                {
                    this.jsPureName = this.jsName.Substring(0, gArgIndex);
                }
            }
            else
            {
                this.jsModule    = type.Namespace ?? "";
                this.jsNamespace = "";

                // 处理内部类层级
                var declaringType = type.DeclaringType;
                while (declaringType != null)
                {
                    this.jsNamespace = string.IsNullOrEmpty(this.jsNamespace) ? declaringType.Name : $"{declaringType.Name}.{this.jsNamespace}";
                    declaringType    = declaringType.DeclaringType;
                }

                if (type.IsGenericType)
                {
                    this.jsName     = naming.Contains("`") ? naming.Substring(0, naming.IndexOf('`')) : naming;
                    this.jsPureName = this.jsName;

                    if (type.IsGenericTypeDefinition)
                    {
                        if (!naming.Contains("<"))
                        {
                            this.jsName += "<";
                            var gArgs = type.GetGenericArguments();

                            for (var i = 0; i < gArgs.Length; i++)
                            {
                                this.jsName += gArgs[i].Name;
                                if (i != gArgs.Length - 1)
                                {
                                    this.jsName += ", ";
                                }
                            }
                            this.jsName += ">";
                        }
                    }
                    else
                    {
                        foreach (var gp in type.GetGenericArguments())
                        {
                            this.jsName += "_" + gp.Name;
                        }
                    }
                }
                else
                {
                    this.jsName = naming;

                    //TODO: 整理 jsPureName 的取值流程 (对于泛型中的嵌套的处理等)
                    var gArgIndex = this.jsName.IndexOf("<");
                    if (gArgIndex < 0)
                    {
                        this.jsPureName = this.jsName;
                    }
                    else
                    {
                        this.jsPureName = this.jsName.Substring(0, gArgIndex);
                    }
                }
            }

            if (string.IsNullOrEmpty(this.jsNamespace))
            {
                this.jsModuleAccess       = this.jsName;
                this.jsModuleImportAccess = this.jsPureName;
                this.jsLocalName          = "";
            }
            else
            {
                var i = this.jsNamespace.IndexOf('.');
                this.jsModuleAccess       = i < 0 ? this.jsNamespace : this.jsNamespace.Substring(0, i);
                this.jsModuleImportAccess = this.jsModuleAccess;
                this.jsLocalName          = CodeGenUtils.Concat(".", i < 0 ? "" : this.jsNamespace.Substring(i + 1), this.jsName);
            }

            if (this.jsModuleAccess.EndsWith("[]"))
            {
                this.jsModuleAccess = this.jsModuleAccess.Substring(0, this.jsModuleAccess.Length - 2);
            }

            this.jsDepth                  = this.jsModuleAccess.Split('.').Length;
            this.jsFullName               = CodeGenUtils.Concat(".", jsModule, jsNamespace, this.jsName);
            this.jsNamespaceSlice         = jsNamespace.Split('.');
            this.jsNameNormalized         = CodeGenUtils.Normalize(this.jsName);
            this.jsFullNameForReflectBind = CodeGenUtils.NormalizeEx(jsNamespaceSlice, CodeGenUtils.Normalize(jsName));
        }
コード例 #6
0
 public virtual void OnPostGenerateType(BindingManager bindingManager, TypeBindingInfo bindingInfo)
 {
 }
コード例 #7
0
 public virtual void OnPostGenerateDelegate(BindingManager bindingManager, DelegateBridgeBindingInfo bindingInfo)
 {
 }
コード例 #8
0
 public virtual bool OnExportingType(BindingManager bindingManager, Type type)
 {
     return(false);
 }
コード例 #9
0
 public virtual void OnPostCollectTypes(BindingManager bindingManager)
 {
 }
コード例 #10
0
 public virtual void OnPostExporting(BindingManager bindingManager)
 {
 }
コード例 #11
0
 public virtual void OnPostCollectAssemblies(BindingManager bindingManager)
 {
 }
コード例 #12
0
 public virtual void OnInitialize(BindingManager bindingManager)
 {
 }