/// <summary> /// 作成した型からの値の取り出し /// </summary> /// <param name="obj">取得対象</param> /// <returns>取得結果</returns> public Dictionary <string, object> GetDictionary(object obj) { Type objectType = obj.GetType(); if (RetType != objectType) { throw new Exception("対象外の型のインスタンスが指定されています"); } Dictionary <string, object> retVal = new Dictionary <string, object>(); foreach (PropertyInfo pInfo in RetType.GetProperties()) { string propertyName = pInfo.Name; object value = RetType.InvokeMember(propertyName, BindingFlags.GetProperty, null, obj, new object[] { }); retVal.Add(propertyName, value); } return(retVal); }
/// <summary> /// 作成した型への値の格納 /// </summary> /// <param name="obj">格納対象</param> /// <param name="data">格納するデータ</param> public void SetDictionary(object obj, Dictionary <string, object> data) { Type objectType = obj.GetType(); if (RetType != objectType) { throw new Exception("対象外の型のインスタンスが指定されています"); } foreach (PropertyInfo pInfo in RetType.GetProperties()) { string propertyName = pInfo.Name; if (data.ContainsKey(propertyName)) { if (data[propertyName].GetType() != pInfo.PropertyType) { throw new Exception("次のプロパティの型が異なります:" + propertyName); } RetType.InvokeMember(propertyName, BindingFlags.SetProperty, null, obj, new object[] { data[propertyName] }); } } }