この質問は既にSharePointの部分に誤って投稿しました。
あるモデルを別のモデルにマップする必要があります。すべてが正常に機能しますが、最後のプロパティが TargetParameterCountException をスローします。例外をスローするプロパティは「Item」と呼ばれ、このプロパティは私が定義したものではありません。これは辞書のプロパティであると想定しています。
私はすでに1つだけではなく5つのパラメーターすべてを使用しようとしました(ここで説明されているようにMoq + Unit Testing - System.Reflection.TargetParameterCountException: Parameter count mismatch )が、残念ながら同じ例外が発生します。誰かが私を助けてくれたら本当にありがたいです。
敬具と感謝
サンドロ
これはソース モデルの抜粋です。他のすべてのプロパティはまったく同じ方法で実装されます。
public class DataModel : Dictionary<string, object> {}
public class DiscussionDataModel : DataModel
{
public DiscussionDataModel(Dictionary dictionary) : base(dictionary){}
public FieldUserValue Author
{
get { return (FieldUserValue) this["Author"]; }
set { this["Author"] = value; }
}
public double AverageRating
{
get { return (double) this["AverageRating"]; }
set { this["AverageRating"] = value; }
}
}
これはターゲット モデルの抜粋です。他のすべてのプロパティはまったく同じ方法で実装されます。
public class DiscussionModel : BaseModel
{
public FieldUserValue Author { get; set; }
public double AverageRating { get; set; }
}
これは、DataModel を BaseModel にマップするための一般的な拡張メソッドです。
public static T ToModel(this DataModel dataModel) where T : BaseModel
{
try
{
T model = Activator.CreateInstance();
if (dataModel != null)
{
PropertyInfo[] propertyInfos = dataModel.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo propertyInfo in propertyInfos)
{
object value = propertyInfo.GetValue(dataModel);
if (value == null) { break; }
PropertyInfo modelPropertyInfo = model.GetType().GetProperty(propertyInfo.Name);
modelPropertyInfo?.SetValue(model, value);
}
return model;
}
}
catch (Exception ex)
{
throw;
}
return null;
}