public TypeBindingInfo(BindingManager bindingManager, Type type, TypeBindingFlags bindingFlags) { this.bindingManager = bindingManager; this.type = type; this.bindingFlags = bindingFlags; this.transform = bindingManager.GetTypeTransform(type); var naming = this.transform?.GetTypeNaming() ?? GetNamingAttribute(type); var indexOfTypeName = naming.LastIndexOf('.'); if (indexOfTypeName >= 0) // 内部类 { this.jsNamespace = naming.Substring(0, indexOfTypeName); this.jsName = naming.Substring(indexOfTypeName + 1); } else { if (type.DeclaringType != null) { if (string.IsNullOrEmpty(type.Namespace)) { this.jsNamespace = type.DeclaringType.Name; } else { this.jsNamespace = $"{type.Namespace}.{type.DeclaringType.Name}"; } } else { this.jsNamespace = type.Namespace ?? ""; } if (type.IsGenericType) { this.jsName = naming.Substring(0, naming.IndexOf('`')); foreach (var gp in type.GetGenericArguments()) { this.jsName += "_" + gp.Name; } } else { this.jsName = naming; } } this.name = bindingManager.prefs.typeBindingPrefix + (this.jsNamespace + "_" + this.jsName).Replace('.', '_').Replace('+', '_').Replace('<', '_').Replace('>', '_'); this.constructors = new ConstructorBindingInfo(type); }
public string AppendMethodReturnValuePusher(MethodBase method, Type returnType, string value) { var transform = bindingManager.GetTypeTransform(method.DeclaringType); if (transform != null) { var mrp = transform.GetMethodReturnPusher(method); if (mrp != null) { return($"{mrp}(ctx, {value})"); } } if (returnType.IsEnum) { var eType = returnType.GetEnumUnderlyingType(); var eTypeName = this.bindingManager.GetCSTypeFullName(eType); return($"{this.bindingManager.GetScriptObjectPusher(eType)}(ctx, ({eTypeName}){value})"); } return($"{this.bindingManager.GetScriptObjectPusher(returnType)}(ctx, {value})"); }
// 收集所有 字段,属性,方法 public void Collect() { 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.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 != null && transform.IsMemberBlocked(field.Name)) { bindingManager.Info("skip blocked 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.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 != null && transform.IsMemberBlocked(evt.Name)) { bindingManager.Info("skip blocked 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; } 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 != null && transform.IsMemberBlocked(property.Name)) { bindingManager.Info("skip blocked property: {0}", property.Name); continue; } //NOTE: 索引访问 if (property.Name == "Item") { if (property.CanRead && property.GetMethod != null) { if (BindingManager.IsUnsupported(property.GetMethod)) { bindingManager.Info("skip unsupported get-method: {0}", property.GetMethod); continue; } AddMethod(property.GetMethod, true, "$GetValue"); } if (property.CanWrite && property.SetMethod != null) { if (BindingManager.IsUnsupported(property.SetMethod)) { bindingManager.Info("skip unsupported set-method: {0}", property.SetMethod); continue; } AddMethod(property.SetMethod, true, "$SetValue"); } // 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; } AddConstructor(constructor); } } CollectMethods(type.GetMethods(bindingFlags)); CollectMethods(bindingManager.GetTypeTransform(type).extensionMethods); }