public EventBindingInfo(Type declaringType, EventInfo eventInfo) { this.declaringType = declaringType; this.eventInfo = eventInfo; do { if (this.isStatic) { this.adderName = "BindStaticAdd_" + eventInfo.Name; this.removerName = "BindStaticRemove_" + eventInfo.Name; } else { this.adderName = "BindAdd_" + eventInfo.Name; this.removerName = "BindRemove_" + eventInfo.Name; this.proxyName = "BindProxy_" + eventInfo.Name; } } while (false); this.regName = TypeBindingInfo.GetNamingAttribute(eventInfo); }
// 获取实现的接口的ts声明 public string GetTSInterfacesName(TypeBindingInfo typeBindingInfo) { var interfaces = typeBindingInfo.type.GetInterfaces(); var str = ""; foreach (var @interface in interfaces) { var interfaceBindingInfo = GetExportedType(@interface); if (interfaceBindingInfo != null) { // Debug.Log($"{typeBindingInfo.type.Name} implements {@interface.Name}"); str += GetTSTypeFullName(interfaceBindingInfo.type) + ", "; } } if (str.Length > 0) { str = str.Substring(0, str.Length - 2); } return(str); }
public void AddMethod(MethodInfo methodInfo, bool isIndexer, string renameRegName) { if (this.transform != null) { if (this.transform.IsBlocked(methodInfo)) { bindingManager.Info("skip blocked method: {0}", methodInfo.Name); return; } } var isStatic = methodInfo.IsStatic && !methodInfo.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute)); var group = isStatic ? staticMethods : methods; MethodBindingInfo overrides; var methodName = TypeBindingInfo.GetNamingAttribute(methodInfo); if (!group.TryGetValue(methodName, out overrides)) { overrides = new MethodBindingInfo(isIndexer, isStatic, methodName, renameRegName ?? methodName); group.Add(methodName, overrides); } overrides.Add(methodInfo); CollectDelegate(methodInfo); bindingManager.Info("[AddMethod] {0}.{1}", type, methodInfo); }
// 生成类型绑定 public void Generate(TypeBindingInfo typeBindingInfo) { using (new PlatformCodeGen(this)) { using (new TopLevelCodeGen(this, typeBindingInfo)) { using (new NamespaceCodeGen(this, this.bindingManager.prefs.ns, typeBindingInfo.jsNamespace)) { if (typeBindingInfo.IsEnum) { using (new EnumCodeGen(this, typeBindingInfo)) { } } else { using (new ClassCodeGen(this, typeBindingInfo)) { } } } } } }
public virtual void OnPostGenerateType(BindingManager bindingManager, TypeBindingInfo bindingInfo) { }
public FieldBindingInfo(FieldInfo fieldInfo) { do { if (fieldInfo.IsLiteral) { try { var cv = fieldInfo.GetRawConstantValue(); var cvType = cv.GetType(); if (cvType == typeof(string)) { constantValue = $"\"{cv}\""; break; } if (cvType == typeof(int) || cvType == typeof(uint) || cvType == typeof(byte) || cvType == typeof(sbyte) || cvType == typeof(short) || cvType == typeof(ushort) || cvType == typeof(bool)) { constantValue = $"{cv}"; break; } if (cvType == typeof(float)) { var fcv = (float)cv; if (!float.IsInfinity(fcv) && !float.IsNaN(fcv)) { constantValue = $"{cv}"; break; } } // if (cvType.IsPrimitive && cvType.IsValueType) // { // constantValue = $"{cv}"; // break; // } } catch (Exception) { } } if (fieldInfo.IsStatic) { this.getterName = "BindStaticRead_" + fieldInfo.Name; if (!fieldInfo.IsInitOnly && !fieldInfo.IsLiteral) { this.setterName = "BindStaticWrite_" + fieldInfo.Name; } } else { this.getterName = "BindRead_" + fieldInfo.Name; if (!fieldInfo.IsInitOnly && !fieldInfo.IsLiteral) { this.setterName = "BindWrite_" + fieldInfo.Name; } } } while (false); this.regName = TypeBindingInfo.GetNamingAttribute(fieldInfo); this.fieldInfo = fieldInfo; }
public ClassCodeGen(CodeGenerator cg, TypeBindingInfo bindingInfo) : base(cg, bindingInfo) { this.cg.AppendJSDoc(this.bindingInfo.type); var transform = this.bindingInfo.transform; var prefix = this.bindingInfo.jsNamespace != null ? "" : "declare "; var super = this.cg.bindingManager.GetTSSuperName(this.bindingInfo); var interfaces = this.cg.bindingManager.GetTSInterfacesName(this.bindingInfo); var extends = string.IsNullOrEmpty(super) ? "" : $" extends {super}"; var implements = string.IsNullOrEmpty(interfaces) ? "" : $" implements {interfaces}"; var regName = this.bindingInfo.jsName; if (bindingInfo.type.IsAbstract) { prefix += "abstract "; } this.cg.tsDeclare.AppendLine($"{prefix}class {regName}{extends}{implements} {{"); this.cg.tsDeclare.AddTabLevel(); // 生成函数体 // 构造函数 if (this.bindingInfo.constructors.available) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, this.bindingInfo.constructors.name)) { using (new TryCatchGuradCodeGen(cg)) { using (new ConstructorCodeGen(cg, this.bindingInfo)) { } } } } } // 非静态成员方法 foreach (var kv in this.bindingInfo.methods) { var methodBindingInfo = kv.Value; if (transform == null || !transform.IsRedirectedMethod(methodBindingInfo.regName)) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, methodBindingInfo.name)) { using (new TryCatchGuradCodeGen(cg)) { using (new MethodCodeGen(cg, methodBindingInfo)) { } } } } } using (new TSMethodCodeGen(cg, methodBindingInfo)) { } } //TODO: C# 抽象类可以不提供方法实现, d.ts 需要补充声明 // if (this.bindingInfo.type.IsAbstract && !this.bindingInfo.type.IsInterface) // { // } // 静态成员方法 foreach (var kv in this.bindingInfo.staticMethods) { var methodBindingInfo = kv.Value; if (transform == null || !transform.IsRedirectedMethod(methodBindingInfo.regName)) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, methodBindingInfo.name)) { using (new TryCatchGuradCodeGen(cg)) { using (new MethodCodeGen(cg, methodBindingInfo)) { } } } } } using (new TSMethodCodeGen(cg, methodBindingInfo)) { } } // 所有附加方法 if (transform != null) { transform.ForEachAdditionalTSMethodDeclaration(decl => { this.cg.tsDeclare.AppendLine(decl); }); } // 所有属性 foreach (var kv in this.bindingInfo.properties) { var propertyBindingInfo = kv.Value; // 静态 if (propertyBindingInfo.staticPair.IsValid()) { // 可读属性 if (propertyBindingInfo.staticPair.getterName != null) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, propertyBindingInfo.staticPair.getterName)) { using (new TryCatchGuradCodeGen(cg)) { using (new PropertyGetterCodeGen(cg, propertyBindingInfo)) { } } } } } // 可写属性 if (propertyBindingInfo.staticPair.setterName != null) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, propertyBindingInfo.staticPair.setterName)) { using (new TryCatchGuradCodeGen(cg)) { using (new PropertySetterCodeGen(cg, propertyBindingInfo)) { } } } } } } // 非静态 if (propertyBindingInfo.instancePair.IsValid()) { // 可读属性 if (propertyBindingInfo.instancePair.getterName != null) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, propertyBindingInfo.instancePair.getterName)) { using (new TryCatchGuradCodeGen(cg)) { using (new PropertyGetterCodeGen(cg, propertyBindingInfo)) { } } } } } // 可写属性 if (propertyBindingInfo.instancePair.setterName != null) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, propertyBindingInfo.instancePair.setterName)) { using (new TryCatchGuradCodeGen(cg)) { using (new PropertySetterCodeGen(cg, propertyBindingInfo)) { } } } } } } } // 所有字段 foreach (var kv in this.bindingInfo.fields) { var fieldBindingInfo = kv.Value; if (fieldBindingInfo.getterName != null) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, fieldBindingInfo.getterName)) { using (new TryCatchGuradCodeGen(cg)) { using (new FieldGetterCodeGen(cg, fieldBindingInfo)) { } } } } } // 可写字段 if (fieldBindingInfo.setterName != null) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, fieldBindingInfo.setterName)) { using (new TryCatchGuradCodeGen(cg)) { using (new FieldSetterCodeGen(cg, fieldBindingInfo)) { } } } } } } // 所有事件 (当做field相似处理) foreach (var kv in this.bindingInfo.events) { var eventBindingInfo = kv.Value; using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, eventBindingInfo.adderName)) { using (new TryCatchGuradCodeGen(cg)) { using (new EventAdderCodeGen(cg, eventBindingInfo)) { } } } } using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, eventBindingInfo.removerName)) { using (new TryCatchGuradCodeGen(cg)) { using (new EventRemoverCodeGen(cg, eventBindingInfo)) { } } } } if (!eventBindingInfo.isStatic) { using (new PInvokeGuardCodeGen(cg)) { using (new BindingFuncDeclareCodeGen(cg, eventBindingInfo.proxyName)) { using (new TryCatchGuradCodeGen(cg)) { using (new EventProxyCodeGen(cg, eventBindingInfo)) { } } } } } } }