/// <summary>根据PatchInfo转换成Patch /// </summary> public Patch InfoToPatch(PatchInfo info) { if (info.StoreType == StoreType.S3) { var s3Patch = new S3Patch() { StoreType = (int)StoreType.S3, Name = info.Name, Code = info.Code, State = info.State }; if (info.Expands.Any()) { var bucketDict = info.Expands[0].ToDictionary(x => x.Key, x => x.Value); s3Patch.Bucket = DictionaryEmit.DictionaryToObject <BucketInfo>(bucketDict); } return(s3Patch); } else if (info.StoreType == StoreType.FastDFS) { var fastDFSPatch = new FastDFSPatch() { StoreType = (int)StoreType.FastDFS, Name = info.Name, Code = info.Code, State = info.State }; foreach (var item in info.Expands) { var trackerDict = item.ToDictionary(x => x.Key, x => x.Value); var tracker = DictionaryEmit.DictionaryToObject <Tracker>(trackerDict); fastDFSPatch.Trackers.Add(tracker); } return(fastDFSPatch); } else { throw new ArgumentException("不是有效的Patch类型,无法转换成Patch"); } }
public void ObjectToDictionary_DictionaryToObject_Test() { Assert.Throws <NotSupportedException>(() => { DictionaryEmit.DictionaryToObject <List <int> >(new Dictionary <string, string>()); }); Assert.Throws <NotSupportedException>(() => { DictionaryEmit.GetDictionaryFunc <List <DictionaryEmitClass1> >(); }); var o = new DictionaryEmitClass1() { Id = 100, Name = "zhangsan", Age = 22, Birthday = new DateTime(2020, 3, 5) }; var dict1 = DictionaryEmit.ObjectToDictionary <DictionaryEmitClass1>(o); Assert.Equal(o.Id.ToString(), dict1["Id"]); Assert.Equal(o.Name, dict1["Name"]); Assert.Equal(o.Age?.ToString(), dict1["Age"]); Assert.Equal(4, dict1.Count); var o1 = DictionaryEmit.DictionaryToObject <DictionaryEmitClass1>(dict1); Assert.Equal(o.Id, o1.Id); Assert.Equal(o.Name, o1.Name); Assert.Equal(o.Age, o1.Age); Assert.Equal(o.Birthday, o1.Birthday); }