void RegFunction(Type t, StreamWriter file) { // Write export function Write(file, "static public void reg(IntPtr l) {"); if (t.BaseType != null && t.BaseType.Name.Contains("UnityEvent`")) { Write(file, "LuaUnityEvent_{1}.reg(l);", FullName(t), _Name((GenericName(t.BaseType)))); } Write(file, "getTypeTable(l,\"{0}\");", FullName(t)); foreach (string f in funcname) { Write(file, "addMember(l,{0});", f); } foreach (string f in directfunc.Keys) { bool instance = directfunc[f]; Write(file, "addMember(l,{0},{1});", f, instance?"true":"false"); } foreach (string f in propname.Keys) { PropPair pp = propname[f]; Write(file, "addMember(l,\"{0}\",{1},{2},{3});", f, pp.get, pp.set, pp.isInstance?"true":"false"); } if (t.BaseType != null && !CutBase(t.BaseType)) { if (t.BaseType.Name.Contains("UnityEvent`1")) { Write(file, "createTypeMetatable(l,constructor, typeof({0}),typeof(LuaUnityEvent_{1}));", FullName(t), _Name(GenericName(t.BaseType))); } else { Write(file, "createTypeMetatable(l,constructor, typeof({0}),typeof({1}));", FullName(t), FullName(t.BaseType)); } } else { Write(file, "createTypeMetatable(l,constructor, typeof({0}));", FullName(t)); } //Write(file, "LuaDLL.lua_pop(l, 1);"); Write(file, "}"); }
private void WriteField(Type t, StreamWriter file) { // Write field set/get FieldInfo[] fields = t.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (FieldInfo fi in fields) { if (DontExport(fi) || IsObsolete(fi)) continue; PropPair pp = new PropPair(); pp.isInstance = !fi.IsStatic; if (fi.FieldType.BaseType != typeof(MulticastDelegate)) { WriteFunctionAttr(file); Write(file, "static public int get_{0}(IntPtr l) {{", fi.Name); WriteTry(file); if (fi.IsStatic) { WritePushValue(fi.FieldType, file, string.Format("{0}.{1}", TypeDecl(t), fi.Name)); } else { WriteCheckSelf(file, t); WritePushValue(fi.FieldType, file, string.Format("self.{0}", fi.Name)); } Write(file, "return 1;"); WriteCatchExecption(file); Write(file, "}"); pp.get = "get_" + fi.Name; } if (!fi.IsLiteral && !fi.IsInitOnly) { WriteFunctionAttr(file); Write(file, "static public int set_{0}(IntPtr l) {{", fi.Name); WriteTry(file); if (fi.IsStatic) { Write(file, "{0} v;", TypeDecl(fi.FieldType)); WriteCheckType(file, fi.FieldType, 2); WriteSet(file, fi.FieldType, TypeDecl(t), fi.Name, true); } else { WriteCheckSelf(file, t); Write(file, "{0} v;", TypeDecl(fi.FieldType)); WriteCheckType(file, fi.FieldType, 2); WriteSet(file, fi.FieldType, t.FullName, fi.Name); } if (t.IsValueType && !fi.IsStatic) Write(file, "setBack(l,self);"); Write(file, "return 0;"); WriteCatchExecption(file); Write(file, "}"); pp.set = "set_" + fi.Name; } propname.Add(fi.Name, pp); tryMake(fi.FieldType); } //for this[] List<PropertyInfo> getter = new List<PropertyInfo>(); List<PropertyInfo> setter = new List<PropertyInfo>(); // Write property set/get PropertyInfo[] props = t.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (PropertyInfo fi in props) { //if (fi.Name == "Item" || IsObsolete(fi) || MemberInFilter(t,fi) || DontExport(fi)) if (IsObsolete(fi) || MemberInFilter(t, fi) || DontExport(fi)) continue; if (fi.Name == "Item" || (t.Name == "String" && fi.Name == "Chars")) // for string[] { //for this[] if (!fi.GetGetMethod().IsStatic && fi.GetIndexParameters().Length == 1) { if (fi.CanRead && !IsNotSupport(fi.PropertyType)) getter.Add(fi); if (fi.CanWrite && fi.GetSetMethod() != null) setter.Add(fi); } continue; } PropPair pp = new PropPair(); bool isInstance = true; if (fi.CanRead && fi.GetGetMethod() != null) { if (!IsNotSupport(fi.PropertyType)) { WriteFunctionAttr(file); Write(file, "static public int get_{0}(IntPtr l) {{", fi.Name); WriteTry(file); if (fi.GetGetMethod().IsStatic) { isInstance = false; WritePushValue(fi.PropertyType, file, string.Format("{0}.{1}", TypeDecl(t), fi.Name)); } else { WriteCheckSelf(file, t); WritePushValue(fi.PropertyType, file, string.Format("self.{0}", fi.Name)); } Write(file, "return 1;"); WriteCatchExecption(file); Write(file, "}"); pp.get = "get_" + fi.Name; } } if (fi.CanWrite && fi.GetSetMethod() != null) { WriteFunctionAttr(file); Write(file, "static public int set_{0}(IntPtr l) {{", fi.Name); WriteTry(file); if (fi.GetSetMethod().IsStatic) { WriteValueCheck(file, fi.PropertyType, 2); WriteSet(file, fi.PropertyType, TypeDecl(t), fi.Name, true); isInstance = false; } else { WriteCheckSelf(file, t); WriteValueCheck(file, fi.PropertyType, 2); WriteSet(file, fi.PropertyType, TypeDecl(t), fi.Name); } if (t.IsValueType) Write(file, "setBack(l,self);"); Write(file, "return 0;"); WriteCatchExecption(file); Write(file, "}"); pp.set = "set_" + fi.Name; } pp.isInstance = isInstance; propname.Add(fi.Name, pp); tryMake(fi.PropertyType); } //for this[] WriteItemFunc(t, file, getter, setter); }
private void WriteField(Type t, StreamWriter file) { // Write field set/get FieldInfo[] fields = t.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (FieldInfo fi in fields) { PropPair pp = new PropPair(); pp.isInstance = !fi.IsStatic; if (fi.FieldType.BaseType != typeof(MulticastDelegate)) { WriteFunctionAttr(file); Write(file, "static public int get_{0}(IntPtr l) {{", fi.Name); if (fi.IsStatic) { WritePushValue(fi.FieldType, file, string.Format("{0}.{1}", t.FullName, fi.Name)); } else { Write(file, "{0} o = ({0})checkSelf(l);", FullName(t)); WritePushValue(fi.FieldType, file, string.Format("o.{0}", fi.Name)); } Write(file, "return 1;"); Write(file, "}"); pp.get = "get_" + fi.Name; } if (!fi.IsLiteral && !fi.IsInitOnly) { WriteFunctionAttr(file); Write(file, "static public int set_{0}(IntPtr l) {{", fi.Name); if (fi.IsStatic) { Write(file, "{0} v;", TypeDecl(fi.FieldType)); WriteCheckType(file, fi.FieldType, 2); WriteSet(file, fi.FieldType, t.FullName, fi.Name, true); } else { Write(file, "{0} o = ({0})checkSelf(l);", FullName(t)); Write(file, "{0} v;", TypeDecl(fi.FieldType)); WriteCheckType(file, fi.FieldType, 2); WriteSet(file, fi.FieldType, t.FullName, fi.Name); } if (t.IsValueType && !fi.IsStatic) { Write(file, "setBack(l,o);"); } Write(file, "return 0;"); Write(file, "}"); pp.set = "set_" + fi.Name; } propname.Add(fi.Name, pp); tryMake(fi.FieldType); } // Write property set/get PropertyInfo[] props = t.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (PropertyInfo fi in props) { if (fi.Name == "Item" || IsObsolete(fi) || MemberInFilter(t, fi)) { continue; } PropPair pp = new PropPair(); bool isInstance = true; if (fi.CanRead) { WriteFunctionAttr(file); Write(file, "static public int get_{0}(IntPtr l) {{", fi.Name); if (IsNotSupport(fi.PropertyType)) { NotSupport(file); } else { if (fi.GetGetMethod().IsStatic) { isInstance = false; WritePushValue(fi.PropertyType, file, string.Format("{0}.{1}", t.FullName, fi.Name)); } else { Write(file, "{0} o = ({0})checkSelf(l);", FullName(t)); WritePushValue(fi.PropertyType, file, string.Format("o.{0}", fi.Name)); } Write(file, "return 1;"); } Write(file, "}"); pp.get = "get_" + fi.Name; } if (fi.CanWrite && fi.GetSetMethod() != null) { WriteFunctionAttr(file); Write(file, "static public int set_{0}(IntPtr l) {{", fi.Name); if (IsNotSupport(fi.PropertyType)) { NotSupport(file); } else { if (fi.GetSetMethod().IsStatic) { WriteValueCheck(file, fi.PropertyType, 2); WriteSet(file, fi.PropertyType, t.FullName, fi.Name, true); isInstance = false; } else { Write(file, "{0} o = ({0})checkSelf(l);", FullName(t)); WriteValueCheck(file, fi.PropertyType, 2); WriteSet(file, fi.PropertyType, t.FullName, fi.Name); } if (t.IsValueType) { Write(file, "setBack(l,o);"); } Write(file, "return 0;"); } Write(file, "}"); pp.set = "set_" + fi.Name; } pp.isInstance = isInstance; propname.Add(fi.Name, pp); tryMake(fi.PropertyType); } }
/// <summary> /// 创建映射配置 /// </summary> /// <typeparam name="TDestion">目标对象</typeparam> /// <typeparam name="TSource">源对象</typeparam> /// <param name="des"></param> /// <param name="sour"></param> public static void CreateMap <TDestion, TSource>() { var sourceAutomap = (AutoMapAttribute)typeof(TDestion).GetCustomAttributes(false).Where(e => e.GetType() == typeof(AutoMapAttribute)).Where(x => ((AutoMapAttribute)x).Source == typeof(TSource)).FirstOrDefault(); TypePair tp = CreateTypePair <TDestion, TSource>(); List <PropPair> pp = new List <PropPair>(); var desProps = tp.TDestion.GetProperties(); var sourProps = tp.TSource.GetProperties(); if (sourceAutomap == null) { //如果TDestion没有指定TSource foreach (var prop in desProps) { if (tp.TSource.GetProperty(prop.Name) != null) { var pPair = new PropPair { DesPropName = prop.Name, SourPropName = prop.Name }; pp.Add(pPair); } } LockDictionar.GetOrAdd(tp, pp); CreateMap <TSource, TDestion>(); return; } foreach (var prop in desProps) { var mapToOrFromSource = (MapToOrFromPropertyAttribute)prop.GetCustomAttributes(false).Where(e => e.GetType() == typeof(MapToOrFromPropertyAttribute)).Where(x => ((MapToOrFromPropertyAttribute)x).Source == tp.TSource).FirstOrDefault(); if (mapToOrFromSource != null) { var sourProp = tp.TSource.GetProperty(mapToOrFromSource.PropName); if (sourProp != null && prop.PropertyType == sourProp.PropertyType) { var pPair = new PropPair { DesPropName = prop.Name, SourPropName = mapToOrFromSource.PropName }; pp.Add(pPair); } } else { if (tp.TSource.GetProperty(prop.Name) != null) { var pPair = new PropPair { DesPropName = prop.Name, SourPropName = prop.Name }; pp.Add(pPair); } } } LockDictionar.GetOrAdd(tp, pp); if (sourceAutomap.Reverse) { TypePair reverTP = CreateTypePair <TSource, TDestion>(); List <PropPair> reverPP = new List <PropPair>(); foreach (var item in pp) { reverPP.Add(new PropPair { DesPropName = item.SourPropName, SourPropName = item.DesPropName }); } LockDictionar.GetOrAdd(reverTP, reverPP); } }
/// <summary> /// 创建tSource与tDestion之间的映射关系 /// </summary> /// <param name="tDestion"></param> /// <param name="tSource"></param> public static void CreateMap(Type tDestion, Type tSource) { var sourceAutomap = (AutoMapAttribute)tDestion.GetCustomAttributes().Where(e => e.GetType() == typeof(AutoMapAttribute)).Where(x => ((AutoMapAttribute)x).Source == tSource).FirstOrDefault(); TypePair tp = new TypePair(tDestion, tSource); List <PropPair> pp = new List <PropPair>(); var desProps = tp.TDestion.GetProperties(); var sourProps = tp.TSource.GetProperties(); if (sourceAutomap == null) { //tDestion 与tSource之间不存在映射关系 foreach (var prop in desProps) { if (tp.TSource.GetProperty(prop.Name) != null) { var pPair = new PropPair { DesPropName = prop.Name, SourPropName = prop.Name }; pp.Add(pPair); } } LockDictionar.GetOrAdd(tp, pp); CreateMap(tSource, tDestion); return; } foreach (var prop in desProps) { if (prop.PropertyType == typeof(IEnumerable <>)) { //暂不转换TDestion中的集合类型对象 continue; } var mapToOrFromSource = (MapToOrFromPropertyAttribute)prop.GetCustomAttributes(false).Where(e => e.GetType() == typeof(MapToOrFromPropertyAttribute)).Where(x => ((MapToOrFromPropertyAttribute)x).Source == tp.TSource).FirstOrDefault(); if (mapToOrFromSource != null) { var sourProp = tp.TSource.GetProperty(mapToOrFromSource.PropName); if (sourProp != null && prop.PropertyType == sourProp.PropertyType) { var pPair = new PropPair { DesPropName = prop.Name, SourPropName = mapToOrFromSource.PropName }; pp.Add(pPair); } } else { if (tp.TSource.GetProperty(prop.Name) != null) { var pPair = new PropPair { DesPropName = prop.Name, SourPropName = prop.Name }; pp.Add(pPair); } } } LockDictionar.GetOrAdd(tp, pp); if (sourceAutomap.Reverse) { TypePair reverTP = new TypePair(tSource, tDestion); List <PropPair> reverPP = new List <PropPair>(); foreach (var item in pp) { reverPP.Add(new PropPair { DesPropName = item.SourPropName, SourPropName = item.DesPropName }); } LockDictionar.GetOrAdd(reverTP, reverPP); } }