C# 泛型

学习中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class UserDefineType1
    {
        public int IntInstance1 {  get; set; }
        public int IntInstance2 {  get; set; }
    }
    class UserDefineType2
    {
        public string StringInstance1 { get; set; }
        public string StringInstance2 { get; set; }
    }

    class GenericBaseClass<T> where T : class
    {
        private T value;
        public int IntProperty { get; set; }
        public string StringProperty { get; set; }

        public GenericBaseClass(int inyParameter,string stringParameter)
        {
            IntProperty = inyParameter;
            StringProperty = stringParameter;
        }   

        public void GetValueFunction()
        {
            PropertyInfo[] objects = value.GetType().GetProperties();//通过反射获取所有属性
            foreach (PropertyInfo obj in objects)
            {
                Console.WriteLine(obj.GetValue(value));//通过反射获取所有属性的值
            }
        }

        public void SetValueFunction(T value)
        {
            this.value = value;
        }
    }

    class GenericClass1<UserDefineType1> : GenericBaseClass<UserDefineType1> where UserDefineType1 : class
    {
        public GenericClass1(int intParameter,string stringParameter):base(intParameter, stringParameter)
        {

        }
    }

    class GenericClass2<UserDefineType2> : GenericBaseClass<UserDefineType2> where UserDefineType2 : class
    {
        public GenericClass2(int intParameter, string stringParameter) : base(intParameter, stringParameter)
        {

        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            GenericClass1<UserDefineType1> genericClass1Instance=new GenericClass1<UserDefineType1>(0,"Hello0");
            GenericClass2<UserDefineType2> genericClass2Instance=new GenericClass2<UserDefineType2>(1,"Hello1");

            UserDefineType1 userDefineType1 = new UserDefineType1();
            UserDefineType2 userDefineType2 = new UserDefineType2();

            userDefineType1.IntInstance1 = 0;
            userDefineType1.IntInstance2 = 1;

            userDefineType2.StringInstance1 = "Hello0";
            userDefineType2.StringInstance2 = "Hello1";

            genericClass1Instance.SetValueFunction(userDefineType1);
            genericClass2Instance.SetValueFunction(userDefineType2);

            genericClass1Instance.GetValueFunction();
            genericClass2Instance.GetValueFunction();

            Console.WriteLine($"{genericClass1Instance.IntProperty},{genericClass1Instance.StringProperty}");
            Console.WriteLine($"{genericClass2Instance.IntProperty},{genericClass2Instance.StringProperty}");




            Console.ReadKey();


        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值