转换ICollection<;界面>;到ICollection<;类别>;[UWP]c#6.0

转换ICollection<;界面>;到ICollection<;类别>;[UWP]c#6.0,c#,interface,uwp,C#,Interface,Uwp,我知道这个问题在堆栈的很多地方都得到了回答,最好的答案是。我试过了,还有很多其他的答案。我有一个库,可以返回接口的集合(IMasterAutoSuggestOutlet) 现在,我想在我的应用程序中将这个接口数据从一个页面传输到另一个页面。由于接口无法序列化,我创建了一个实现此接口的具体类: 我的混凝土课 public class MasterAutoSuggestModel : IMasterAutoSuggestOutlet { public IAddressData Address

我知道这个问题在堆栈的很多地方都得到了回答,最好的答案是。我试过了,还有很多其他的答案。我有一个库,可以返回接口的集合(
IMasterAutoSuggestOutlet

现在,我想在我的应用程序中将这个接口数据从一个页面传输到另一个页面。由于接口无法序列化,我创建了一个实现此接口的具体类:

我的混凝土课

public class MasterAutoSuggestModel : IMasterAutoSuggestOutlet
{
    public IAddressData AddressData { get; set; }

    public IPlaceActivity PlaceActivity { get; set; }

    public string ELoc { get; set; }

    public Geopoint ExactLocation { get; set; }

    public Geopoint EntranceLocation { get; set; }

    public LocationType TypeOfLocation { get; set; }
}
我要做的是,将ICollection转换为ICollection。下面的代码显示了我对该操作的实现:

var collection = mainPageViewModel?.SearchPageVM?.searchManager?.AutoSuggestResponse;
var ob = collection.First();
if (ob is IMasterAutoSuggestOutlet)
{
    var ToBeTransfered = collection.OfType<MasterAutoSuggestModel>();    //Simply returns the collection with a count 0
    var serializedData = JsonConvert.SerializeObject(ToBeTransfered);
    ScenarioFrame.Navigate(typeof(MasterSearchResultPage), serializedData);
}
var collection=mainPageViewModel?.SearchPageVM?.searchManager?.AutoSuggestResponse;
var ob=collection.First();
如果(ob是IMasterAutoSuggestOutlet)
{
var ToBeTransfered=collection.OfType();//只返回计数为0的集合
var serializedData=JsonConvert.SerializeObject(ToBeTransfered);
ScenarioFrame.Navigate(typeof(MasterSearchResultPage),serializedData);
}
问题在于
var ToBeTransfered=col.OfType()
它返回计数为0的集合,即使
集合中有10项


有人能告诉我哪里出了问题吗?请注意,我需要使用此转换的集合进行序列化,并将其作为导航参数发送到下一页

ofType方法按指定的类型过滤连接。如果要从其他库中检索对象,则它们不会是特定的类型。

您可能想做的是将从库中检索到的项转换为dto以进行序列化。您可以使用automapper之类的工具进行转换

if (ob is IMasterAutoSuggestOutlet) {
   var transferObject = new MasterAutoSuggestModel(){
       //Set Properties
    }
   // var ToBeTransfered = collection.OfType<MasterAutoSuggestModel>();    //Simply returns the collection with a count 0
    var serializedData = JsonConvert.SerializeObject(transferObject);
    ScenarioFrame.Navigate(typeof(MasterSearchResultPage), serializedData); }
if(ob是IMasterAutoSuggestOutlet){
var transferObject=新的MasterAutoSuggestModel(){
//设置属性
}
//var ToBeTransfered=collection.OfType();//只返回计数为0的集合
var serializedData=JsonConvert.SerializeObject(transferObject);
ScenarioFrame.Navigate(typeof(MasterSearchResultPage),serializedData);}

我不确定手动设置属性是否是正确的方法,因为在这种情况下,属性要少得多,但当属性超过6个时会发生什么?这就是像AutoMapper这样的东西出现的地方。在安装AutoMapper nugget或导入库并进行映射时,有什么方法吗。我能做一个明确的演员阵容吗?或者为其编写一个扩展方法?如果您可以访问库中的具体类型,则只能将其强制转换为具体类型。当库返回一个接口时,强制转换不是一种方法,因为实际的实现类型可能会改变,从而破坏代码。好的,那么你有一个关于如何在这种情况下使用autoMapper的示例吗?我在使用automapper将接口转换为类时失败得很惨。
if (ob is IMasterAutoSuggestOutlet) {
   var transferObject = new MasterAutoSuggestModel(){
       //Set Properties
    }
   // var ToBeTransfered = collection.OfType<MasterAutoSuggestModel>();    //Simply returns the collection with a count 0
    var serializedData = JsonConvert.SerializeObject(transferObject);
    ScenarioFrame.Navigate(typeof(MasterSearchResultPage), serializedData); }