カテゴリー
C

C#、ジェック型クラスに演算子オーバーロードして見ましたが、四苦八苦しました。

using System;
using System.Linq;

namespace Chapter01All
{   
    class MyGeneric<T>
    {
        public T num1;
        public MyGeneric(T obj1)
        {
            num1 = obj1;
        }
        public T GetNum1()
        {
            return num1;
        }
        public static MyGeneric<T> operator +(MyGeneric<T> kariobj, MyGeneric<T> kari)
        {
            var testClass = new TestClass<T>();
            var myGeneric = new MyGeneric<T>(testClass.AddWithDynamic(kariobj.num1, kari.num1));
            return myGeneric;
        }
    }
    public class GenericOperation<T>
    {
        public GenericOperation()
        {
            var availableT = new Type[]
            {
                typeof(int), typeof(uint), typeof(short), typeof(ushort),
                typeof(long), typeof(ulong), typeof(byte),
                typeof(decimal), typeof(double)
            };
            if (!availableT.Contains(typeof(T)))
                {
                    throw new NotSupportedException();
                }
        }
    }
 
    class TestClass<T>
    {
        public T AddWithDynamic(T a, T b)
        {
            return (dynamic)a + (dynamic)b;
        }
    }
    
    class GenericDemo
    {
        static void Main()
        {
            MyGeneric<int> intobj = new MyGeneric<int>(100);
            MyGeneric<int> intobj2 = new MyGeneric<int>(200);
            MyGeneric<int> ans = intobj + intobj2;
            Console.WriteLine(ans.GetNum1());

            MyGeneric<double> intobj3 = new MyGeneric<double>(1.2345);
            MyGeneric<double> intobj4 = new MyGeneric<double>(10.0001);
            MyGeneric<double> ans2 = intobj3 + intobj4;
            Console.WriteLine(ans2.GetNum1());

        }
    }
}

実行画面は省略しますが、intとdoubleで上手く行ったので、良いんだろうと思います。まあ、面倒ですね。

using System;
using System.Linq;
using System.Linq.Expressions;

namespace Chapter01All
{   
    class MyGeneric<T>
    {
        public T num1;
        public MyGeneric(T obj1)
        {
            num1 = obj1;
        }
        public T GetNum1()
        {
            return num1;
        }
        public static MyGeneric<T> operator +(MyGeneric<T> kariobj, MyGeneric<T> kari)
        {
            var testClass = new TestClass<T>();
            var myGeneric = new MyGeneric<T>(testClass.AddWithDynamic(kariobj.num1, kari.num1));
            return myGeneric;
        }
    }
    public class GenericOperation<T>
    {
        public GenericOperation()
        {
            var availableT = new Type[]
            {
                typeof(int), typeof(uint), typeof(short), typeof(ushort),
                typeof(long), typeof(ulong), typeof(byte),
                typeof(decimal), typeof(double)
            };
            if (!availableT.Contains(typeof(T)))
                {
                    throw new NotSupportedException();
                }
            var p1 = Expression.Parameter(typeof(T));
            var p2 = Expression.Parameter(typeof(T));
            Add = Expression.Lambda<Func<T, T, T>>(Expression.Add(p1, p2), p1, p2).Compile();
            Subtract = Expression.Lambda<Func<T, T, T>>(Expression.Subtract(p1, p2), p1, p2).Compile();
            Multiply = Expression.Lambda<Func<T, T, T>>(Expression.Multiply(p1, p2), p1, p2).Compile();
            Divide = Expression.Lambda<Func<T, T, T>>(Expression.Divide(p1, p2), p1, p2).Compile();
            Modulo = Expression.Lambda<Func<T, T, T>>(Expression.Modulo(p1, p2), p1, p2).Compile();
            Equal = Expression.Lambda<Func<T, T, bool>>(Expression.Equal(p1, p2), p1, p2).Compile();
            GreaterThan = Expression.Lambda<Func<T, T, bool>>(Expression.GreaterThan(p1, p2), p1, p2).Compile();
            GreaterThanOrEqual = Expression.Lambda<Func<T, T, bool>>(Expression.GreaterThanOrEqual(p1, p2), p1, p2).Compile();
            LessThan = Expression.Lambda<Func<T, T, bool>>(Expression.LessThan(p1, p2), p1, p2).Compile();
            LessThanOrEqual = Expression.Lambda<Func<T, T, bool>>(Expression.LessThanOrEqual(p1, p2), p1, p2).Compile();

        }
        public Func<T, T, T> Add { get; private set; }
        public Func<T, T, T> Subtract { get; private set; }
        public Func<T, T, T> Multiply { get; private set; }
        public Func<T, T, T> Divide { get; private set; }
        public Func<T, T, T> Modulo { get; private set; }
        public Func<T, T, bool> Equal { get; private set; }
        public Func<T, T, bool> GreaterThan { get; private set; }
        public Func<T, T, bool> GreaterThanOrEqual { get; private set; }
        public Func<T, T, bool> LessThan { get; private set; }
        public Func<T, T, bool> LessThanOrEqual { get; private set; }

    }

    class TestClass<T>
    {
        public T AddWithDynamic(T a, T b)
        {
            return (dynamic)a + (dynamic)b;
        }
    }
    
    class GenericDemo
    {
        static void Main()
        {
            MyGeneric<int> intobj = new MyGeneric<int>(100);
            MyGeneric<int> intobj2 = new MyGeneric<int>(200);
            MyGeneric<int> ans = intobj + intobj2;
            Console.WriteLine(ans.GetNum1());

            MyGeneric<double> intobj3 = new MyGeneric<double>(1.2345);
            MyGeneric<double> intobj4 = new MyGeneric<double>(10.0001);
            MyGeneric<double> ans2 = intobj3 + intobj4;
            Console.WriteLine(ans2.GetNum1());

        }
    }
}

 class GenericOperationについては、拝借したので、どうやって使うのかも分かりません。少し調べてみようと思います。Expressionについてはエラーを回避する方法が分かりました。using System.linq.Expression;を入れてやればエラー出ません。内容については、苦手なラムダ式と関係してるようです。”(-“”-)”

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

inserted by FC2 system