Ejemplo n.º 1
0
        private void CheckGenericType()
        {
            GenericsBase generics = null;

            //generics = new Generics<Employee, Employee>();
            generics = new InheritedGenerics();

            var type = generics.GetType();

            Console.WriteLine(type);
            //Console.WriteLine(type.GetGenericTypeDefinition());

            var geneTypes = new List <Type>()
            {
                typeof(Generics <>),
                typeof(Generics <,>),
                typeof(Generics <, ,>),
            };
            //var genetype1 = typeof(Generics<>);
            //var genetype2 = typeof(Generics<,>);
            //var genetype3 = typeof(Generics<, ,>);


            Type genetype = null;

            while (type.IsGenericType == false)
            {
                if (type == null)
                {
                    return;
                }

                type = type.BaseType;
            }

            Console.WriteLine("\r\nテスト開始");
            Console.WriteLine(type);
            foreach (var item in geneTypes)
            {
                Console.WriteLine(item);
                //Console.WriteLine(type.IsAssignableFrom(item));
                //Console.WriteLine(item.IsAssignableFrom(type));

                //if (item == type)
                //if (item == type.GetGenericTypeDefinition())
                //if (item.IsAssignableFrom(type))
                if (type.IsAssignableFrom(item))
                {
                    Console.WriteLine("一致");
                }
            }
        }
Ejemplo n.º 2
0
        //private bool IsMatchGeneType2<T_Gene, T_Mem>(T_Gene gene)
        //	where T_Gene : GenericsBase
        //	where T_Mem : new()
        //{
        //	bool isMatch = false;

        //	switch (gene)
        //	{
        //		case InheritedGenerics temp:
        //			Console.WriteLine(temp.Member);
        //			isMatch = true;
        //			break;
        //		case Generics<T_Mem, T_Mem, T_Mem> temp:
        //			Console.WriteLine(temp.Member);
        //			isMatch = true;
        //			break;
        //		case Generics<T_Mem, T_Mem> temp:
        //			Console.WriteLine(temp.Member);
        //			isMatch = true;
        //			break;
        //		case Generics<T_Mem> temp:
        //			Console.WriteLine(temp.Member);
        //			isMatch = true;
        //			break;
        //		case GenericsBase temp:
        //			Console.WriteLine(temp.Member);
        //			isMatch = true;
        //			break;
        //		default:
        //			break;

        //	}
        //	return isMatch;
        //}

        private bool IsMatchGeneType <T_Mem>(GenericsBase gene)
            where T_Mem : new()
        {
            // C#7.0以下ではコンパイルエラー
            bool isMatch = false;

            switch (gene)
            {
            case InheritedGenerics temp:
                Console.WriteLine(temp.Member);
                isMatch = true;
                break;

            case Generics <T_Mem, T_Mem, T_Mem> temp:
                Console.WriteLine(temp.Member);
                isMatch = true;
                break;

            case Generics <T_Mem, T_Mem> temp:
                Console.WriteLine(temp.Member);
                isMatch = true;
                break;

            case Generics <T_Mem> temp:
                Console.WriteLine(temp.Member);
                isMatch = true;
                break;

            case GenericsBase temp:
                Console.WriteLine(temp.Member);
                isMatch = true;
                break;

            default:
                break;
            }
            return(isMatch);
        }