使用转换运算符对集合进行类型转换

发布于 2024-09-05 21:00:46 字数 759 浏览 6 评论 0原文

下面的代码给了我用户定义的转换必须转换为封闭类型或从封闭类型转换,而代码段 #2 则没有...似乎 用户定义的转换例程必须与包含该例程的类相互转换。

我有哪些替代方案?显式运算符作为扩展方法?还要别的吗?

public static explicit operator ObservableCollection<ViewModel>(ObservableCollection<Model> modelCollection)
{
    var viewModelCollection = new ObservableCollection<ViewModel>();

    foreach (var model in modelCollection)
    {
        viewModelCollection.Add(new ViewModel() { Model = model });
    }

    return viewModelCollection;
}

片段 #2

public static explicit operator ViewModel(Model model)
{
    return new ViewModel() {Model = model};
}

提前致谢!

The below code gives me User-defined conversion must convert to or from enclosing type, while snippet #2 doesn't... It seems that a user-defined conversion routine must convert to or from the class that contains the routine.

What are my alternatives? Explicit operator as extension method? Anything else?

public static explicit operator ObservableCollection<ViewModel>(ObservableCollection<Model> modelCollection)
{
    var viewModelCollection = new ObservableCollection<ViewModel>();

    foreach (var model in modelCollection)
    {
        viewModelCollection.Add(new ViewModel() { Model = model });
    }

    return viewModelCollection;
}

Snippet #2

public static explicit operator ViewModel(Model model)
{
    return new ViewModel() {Model = model};
}

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甩你一脸翔 2024-09-12 21:00:46

我建议您使用以下方法转换集合:

var viewModelCollection = new ObservableCollection<ViewModel>(modelCollection.Cast<ViewModel>());

如果您喜欢扩展,您可以定义类似的内容(以避免先前代码中的新内容):

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> coll)
{
   return new ObservableCollection<T>(coll);
}

或者也许,一次完成所有操作:

public static ObservableCollection<TNew> CastObservable<TNew,TOld>(this ObservableCollection<TOld> originalColl)
{
   return new ObservableCollection<TNew>(originalColl.Cast<TNew>());
}

显然,所有先前的代码都可以工作,如果你已经定义了代码片段 #2

I'd suggest you to convert the collection using:

var viewModelCollection = new ObservableCollection<ViewModel>(modelCollection.Cast<ViewModel>());

If you like exstensions you could define something like (to avoid the new in the previous code):

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> coll)
{
   return new ObservableCollection<T>(coll);
}

Or maybe, to do everything in one shot:

public static ObservableCollection<TNew> CastObservable<TNew,TOld>(this ObservableCollection<TOld> originalColl)
{
   return new ObservableCollection<TNew>(originalColl.Cast<TNew>());
}

Obviously all the previous codes, will work if you have defined the snippet #2

我三岁 2024-09-12 21:00:46

您的第一个代码片段应该作为 observablecollection 类的 udc 工作。

Your first code snippet should work as a udc on the observablecollection class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文