私はXamarinフォーム(PCLプロジェクト)を使用しようとしています。私のスコープは、(ウェブサービスを通じて公開されている)公共サービスを消費するアプリを作ることです。 XamarinのウェブサイトTODOASMX Exampleを使用しようとしています。問題は、コードされています。このコードのXamarinは重複したオブジェクトを作成します(ディープコピー)
static TodoItem FromASMXServiceTodoItem (ASMXService.TodoItem item)
{
return new TodoItem {
ID = item.ID,
Name = item.Name,
Notes = item.Notes,
Done = item.Done
};
}
スコープはプライベートdomanin(TodoItem)にASMX Webサービス(ASMXService.TodoItem)からデータをコピーすることです。タイプは同じですが、タイプと異なるネームスペース上でタイプが異なります。
私のケースでは、TodoItem型はますます複雑になり、ディープコピーを使用する必要があります。
public static object CloneObject(object objSource)
{
//step : 1 Get the type of source object and create a new instance of that type
Type typeSource = objSource.GetType();
object objTarget = Activator.CreateInstance(typeSource);
//Step2 : Get all the properties of source object type
PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
//Step : 3 Assign all source property to taget object 's properties
foreach (PropertyInfo property in propertyInfo)
{
//Check whether property can be written to
if (property.CanWrite)
{
//Step : 4 check whether property type is value type, enum or string type
if (property.PropertyType.IsValueType || property.PropertyType.IsEnum || property.PropertyType.Equals(typeof(System.String)))
{
property.SetValue(objTarget, property.GetValue(objSource, null), null);
}
//else property type is object/complex types, so need to recursively call this method until the end of the tree is reached
else
{
object objPropertyValue = property.GetValue(objSource, null);
if (objPropertyValue == null)
{
property.SetValue(objTarget, null, null);
}
else
{
property.SetValue(objTarget, CloneObject(objPropertyValue), null);
}
}
}
}
return objTarget;
}
が、コードを実行すると、エラーは次のとおりです: は今、私は深いコピーのために、このコードを使用しようと
System.MissingMethodException:タイプTodoASMX.Droid.MeginetOTAが見つかりませんデフォルトコンストラクタ。 excInfoByLangは[]
さて型TodoASMX.Droid.MeginetOTA.excInfoByLang []は私のためmodificableされていないと私は、この型にデフォルトコンストラクタを追加することはできません。このタイプは、パブリックWebServiceのインポートによって返されます。
すべての回避策(または解決策)が評価されます。
非常に感謝しています。
MP
この型のコンストラクタはどのように見えますか? –