RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 919154
Accepted
i-one
i-one
Asked:2020-12-13 00:51:10 +0000 UTC2020-12-13 00:51:10 +0000 UTC 2020-12-13 00:51:10 +0000 UTC

Cast<T> 用于一组转换为 T 的元素

  • 772

有一个类包含从int类型到此类类型的强制转换运算符

class Item
{
    public int ID;

    public static implicit operator Item(int id)
    {
        return new Item { ID = id };
    }
}

如果你这样写

int id = 1;
Item item = id;

然后类型转换起作用。

现在我想将一组int 转换为一组Items,所以我这样做了

int[] ids = new int[] { 1, 2, 3 };
Item[] items = ids.Cast<Item>().ToArray();

并得到

System.Core.dll 中发生“System.InvalidCastException”类型的未处理异常附加信息:无法将“System.Int32”类型的对象转换为“Item”类型。

用显式替换隐式没有帮助。

解决这个问题的最优雅的方法是什么,以及(最重要的是)为什么Cast<T>不起作用?

c#
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Андрей NOP
    2020-12-13T01:48:41Z2020-12-13T01:48:41Z

    Cast不起作用,因为如果我们打开它的源代码,我们将看到以下内容:

    static IEnumerable<TResult> CastIterator<TResult>(IEnumerable source) {
        foreach (object obj in source) yield return (TResult)obj;
    }
    

    那些。它本质上是等价的

    int id = 1;
    Item item = (Item)(object)id;
    

    当然,这不起作用,因为object它无法转换为您的类型,并且object通常禁止创建用于显式/隐式转换的运算符。

    为了使用您的运算符,编译器必须在编译时看到它并生成调用:

    int[] ids = new int[] { 1, 2, 3 };
    Item[] items = ids.Select(i => (Item)i).ToArray();
    

    对于已经编译的程序集,这将不起作用。

    PS:

    当然,您可以尝试编写自己的强制转换方法:

    public static class MyExtensions
    {
        public static IEnumerable<TOut> MyCast<TIn, TOut>(this IEnumerable<TIn> sourse)
        {
            foreach (TIn e in sourse) yield return (TOut)e;
        }
    }
    

    但这不会编译,因为您不能TIn将任意类型转换为任意TOut.

    您可以尝试对类型添加约束:

    public static class MyExtensions
    {
        public static IEnumerable<TOut> MyCast<TIn, TOut>(
            this IEnumerable<TIn> sourse) where TOut : TIn
        {
            foreach (TIn e in sourse) yield return (TOut)e;
        }
    }
    

    这个类甚至可以编译,但你将无法使用它:

    Item[] items = ids.MyCast<int, Item>().ToArray();
    

    因为它Item不是(继承人)int。


    当然,如果你真的需要一个解决这个问题的方法,以至于你准备好使用反射或动态类型,那么你可以想出某种解决方案,通过它的签名找到一个方法,为它创建一个委托它并缓存它:

    public static class MyExtensions
    {
        public static IEnumerable<TOut> MyCast<TIn, TOut>(this IEnumerable<TIn> sourse)
        {
            var castMethod = CastImpl<TIn, TOut>.CastMethod;
            foreach (TIn e in sourse) yield return castMethod(e);
        }
    
        static class CastImpl<TIn, TOut>
        {
            public static readonly Func<TIn, TOut> CastMethod;
    
            static CastImpl()
            {
                var attr = MethodAttributes.Static
                         | MethodAttributes.Public
                         | MethodAttributes.HideBySig
                         | MethodAttributes.SpecialName;
                var names = new[] { "op_Implicit", "op_Explicit" };
                var method = typeof(TIn).GetMethods()
                    .Concat(typeof(TOut).GetMethods())
                    .Where(mi => mi.Attributes == attr
                        && names.Contains(mi.Name)
                        && mi.ReturnType == typeof(TOut)
                        && mi.GetParameters() is ParameterInfo[] pi
                        && pi.Length == 1
                        && pi[0].ParameterType == typeof(TIn))
                    .FirstOrDefault();
                if (method == null) throw new NotSupportedException(
                    $"Cast {typeof(TIn)} => {typeof(TOut)} not supported!");
                CastMethod = (Func<TIn, TOut>)method.CreateDelegate(typeof(Func<TIn, TOut>));
            }
        }
    }
    

    那么这当然会起作用:

    int[] ids = new int[] { 1, 2, 3 };
    Item[] items = ids.MyCast<int, Item>().ToArray();
    

    Expression由于会自动找到转换方法,因此using看起来会更短:

    public static class MyExtensions
    {
        public static IEnumerable<TOut> MyCast<TIn, TOut>(this IEnumerable<TIn> sourse)
        {
            var castMethod = CastImpl<TIn, TOut>.CastMethod;
            foreach (TIn e in sourse) yield return castMethod(e);
        }
    
        static class CastImpl<TIn, TOut>
        {
            public static readonly Func<TIn, TOut> CastMethod;
    
            static CastImpl()
            {
                var param = Expression.Parameter(typeof(TIn), "i");
                var body = Expression.Convert(param, typeof(TOut));
                var lambda = Expression.Lambda(body, new[] { param });
                CastMethod = (Func<TIn, TOut>)lambda.Compile();
            }
        }
    }
    

    此外,这种方法被证明更加通用,通常的转换将在其中起作用(与前一种不同):

    var cars = items.MyCast<Vehicle, Car>();
    
    • 8
  2. Andrew_STOP_RU_AGRESSION_IN_UA
    2020-12-13T05:12:26Z2020-12-13T05:12:26Z
    int[] ids = new int[] { 1, 2, 3 };
    
    var test = Array.ConvertAll(ids, (p => (Item)p));//Вариант 1
    var test2 = ids.Select(p => (Item)p).ToArray();  //Вариант 2
    

    检查您自己的代码。

    两者都绝对成功。选择你最喜欢的。

    不要忘记包含命名空间:使用 System.Linq

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5