static void CreateModel(IEnumerable <Tier> cols, SmartBuilder build) { var TableName = cols.FirstOrDefault().Table; SmartBuilder m_builder = new SmartBuilder("CacheDynamic"); m_builder.Class("_" + TableName); foreach (var item in cols) { var toType = props[$"({item.Prev}){item.Table}->{item.Field}"]; m_builder.Property(item.Field, toType); } // 是否有子查询 [2021-1-23 zhangbingbin] var next = Table.Where(x => x.Prev == TableName); foreach (var item in next.Select(x => x.Table).Distinct()) { CreateModel(next.Where(x => x.Table == item), m_builder); } m_builder.SaveType(); m_builder.Build(); var momeryType = m_builder.Instance.GetType(); var ListType = typeof(List <>).MakeGenericType(momeryType); build.Property(TableName, ListType); }
/// <summary> /// 深拷贝 /// </summary> /// <typeparam name="TSource">源类型</typeparam> /// <typeparam name="TTarget">目标类型</typeparam> /// <param name="source"></param> /// <returns></returns> public static TTarget MapTo <TSource, TTarget>(this TSource source) where TTarget : class where TSource : class { if (Cache.MapToCache.TryGetValue($"{typeof(TSource).FullName}+{typeof(TTarget).FullName}", out Delegate deleg)) { return(((Func <TSource, TTarget>)deleg)?.Invoke(source)); } deleg = SmartBuilder.DynamicMethod <Func <TSource, TTarget> >(string.Empty, IL => { if (source is IDictionary) { } if (source is Stream) { var _source = IL.NewObject(IL.ArgumentRef <TSource>(0)); var _target = IL.NewObject(new MemoryStream()); _source.Call("CopyTo", _target); _target.Output(); } else if (source.IsClass()) { var _source = IL.NewEntity <TSource>(IL.ArgumentRef <TSource>(0)); var _target = IL.NewEntity <TTarget>(); foreach (var sourceItem in typeof(TSource).GetProperties()) { var targetItem = typeof(TTarget).GetProperty(sourceItem.Name); if (targetItem == null || sourceItem.PropertyType != targetItem.PropertyType) { continue; } _target.SetValue(sourceItem.Name, _source.GetValue(sourceItem.Name)); } _target.Output(); } else { throw new TypeAccessException(); } IL.Return(); }); if (!Cache.MapToCache.TryAdd($"{typeof(TSource).FullName}+{typeof(TTarget).FullName}", deleg)) { throw new ArgumentException(); } return(((Func <TSource, TTarget>)deleg)?.Invoke(source)); }
public Smart(Frame navigationService, ManagementScope scope) { InitializeComponent(); _navigationService = navigationService; _dataContext = new SMARTViewModel(); ISmartBuilder builder = new SmartBuilder(); builder.SetScope(scope) .SetDriveStorage() .SetViewModel(_dataContext) .Build(); this.DataContext = _dataContext; }
static dynamic SequelizeDynamic(this DataTable dt) { props = new Dictionary <string, Type>(); DataSource = dt; foreach (DataColumn dc in dt.Columns) { props.Add(dc.ColumnName, dc.DataType); } builder = new SmartBuilder("CacheDynamic"); builder.Class(dt.TableName); // 主架 [2021-1-23 zhangbingbin] var main = props.Keys.Where(x => x.IndexOf("->", StringComparison.OrdinalIgnoreCase) == -1); foreach (var item in main) { builder.Property(item, props[item]); } // 分割表级 [2021-1-23 zhangbingbin] Table = props.Keys.Where(x => x.IndexOf("->", StringComparison.OrdinalIgnoreCase) > -1).Select(x => { Tier tier = new Tier(); int start = x.IndexOf("(", StringComparison.OrdinalIgnoreCase); int end = x.IndexOf(")", StringComparison.OrdinalIgnoreCase); int talbe = x.IndexOf("->", StringComparison.OrdinalIgnoreCase); tier.Prev = x.Substring(start + 1, end - (start + 1)); tier.Table = x.Substring(end + 1, talbe - (end + 1)); tier.Field = x.Substring(talbe + 2, x.Length - (talbe + 2)); return(tier); }); var Erji = Table.Where(x => x.Prev == dt.TableName); foreach (var item in Erji.Select(x => x.Table).Distinct()) { CreateModel(Erji.Where(x => x.Table == item), builder); } builder.SaveType(); builder.Build(); var instance = Activator.CreateInstance(typeof(List <>) .MakeGenericType(builder.Instance.GetType())); LoadDataSource(instance); return(instance); }
public void EmitTool() { if (emit != null) { emit?.Invoke(); return; } emit = SmartBuilder.DynamicMethod <Func <Model> >(string.Empty, il => { var model = il.NewEntity <Model>(); model["field1"] = il.NewString("测试数据"); model["field2"] = il.NewInt32(1000).AsNullable(); model["field3"] = il.NewDecimal(13165M).AsNullable(); model["field4"] = il.NewDateTime().AsNullable(); model.Output(); il.Return(); }); emit?.Invoke(); }