示例#1
0
    public static void Main()
    {
        var nonamer = new Person(age: 21);

        Console.WriteLine(nonamer);

        var consumer = new Person("준환", 42);
        var incomer  = new Person("신영", 38);

        Console.WriteLine("consumer={0}, incomer={1}",
                          consumer, incomer);

        Console.WriteLine("changing..");
        var swapper = new Person("스와퍼");

        Console.WriteLine(swapper);

        swapper.Swap(ref consumer, ref incomer);
        Console.WriteLine("consumer={0}, incomer={1}",
                          consumer, incomer);


        MyGeneric.Swap <Person>(ref consumer, ref incomer);

        Console.WriteLine("consumer={0}, incomer={1}",
                          consumer, incomer);

        int a = 10;
        int b = 20;

        Console.WriteLine("a={0}, b={1}", a, b);
        MyGeneric.Swap <int>(ref a, ref b);
        Console.WriteLine("a={0}, b={1}", a, b);
    }
示例#2
0
文件: main.cs 项目: yukozh/coreclr
    static void TestGenericVirtualMethod()
    {
        var o = new MyGeneric <String, Object>();

        Assert.AreEqual(o.GenericVirtualMethod <Program, IEnumerable <String> >(),
                        "System.StringSystem.ObjectProgramSystem.Collections.Generic.IEnumerable`1[System.String]");
    }
示例#3
0
 public static MyGeneric <dynamic> ConveretGeneric(this MyGeneric <MyTypedClass1> argument)
 {
     return(new MyGeneric <dynamic>()
     {
         // here you need to assign all needed properties
         Value = argument.Value
     });
 }
示例#4
0
 public MyGeneric <string, decimal, double> GetMyGeneric([FromBody] MyGeneric <string, decimal, double> s)
 {
     return(new MyGeneric <string, decimal, double>
     {
         MyK = s.MyK,
         MyT = s.MyT,
         MyU = s.MyU,
         Status = s.Status,
     });
 }
示例#5
0
 public MyGeneric <string, decimal, Person> GetMyGenericPerson([FromBody] MyGeneric <string, decimal, Person> s)
 {
     return(new MyGeneric <string, decimal, Person>
     {
         MyK = s.MyK,
         MyT = s.MyT,
         MyU = s.MyU,
         Status = s.Status,
     });
 }
    static void Main(string[] args)
    {
        PersistentGenericBag <Foo> magicBag = myMagic <Foo>();

        // call your generic which do some general list related stuff
        MyGeneric <Foo> .trigger(list);

        // call your none generic which do some foo related stuff
        MyNONEGeneric.trigger(list);
    }
示例#7
0
    static void TestGenericOverStruct()
    {
        var o1 = new MyGeneric <String, MyGrowingStruct>();

        Assert.AreEqual(o1.GenericVirtualMethod <MyChangingStruct, IEnumerable <Program> >(),
                        "System.StringMyGrowingStructMyChangingStructSystem.Collections.Generic.IEnumerable`1[Program]");

        var o2 = new MyChildGeneric <MyChangingStruct>();

        Assert.AreEqual(o2.MovedToBaseClass <MyGrowingStruct>(), typeof(List <MyGrowingStruct>).ToString());
        Assert.AreEqual(o2.ChangedToVirtual <MyGrowingStruct>(), typeof(List <MyGrowingStruct>).ToString());
    }
示例#8
0
文件: N3242.cs 项目: zwmyint/Bridge
        public static void TestObjectLiteralOperator()
        {
            // Base variable values to check against:
            string str        = "Hello, World!";
            string str2       = "Different hello, world!";
            int    int_base   = 5;
            float  float_base = 5.2F;
            double dbl_base   = 5.2;

            // String test
            // Binding the class instance to a specified type variable is
            // important to trigger the actual implicit operator.
            string msg = new MyString(str);

            Assert.AreEqual(str, msg, "String");

            // Integer
            int int_instance = new MyInt(int_base);

            Assert.AreEqual(int_base, int_instance, "Integer");

            // Double
            double dbl_instance = new MyDbl(dbl_base);

            Assert.AreEqual(dbl_base, dbl_instance, "Double");

            // Generic as String
            string msg2 = new MyGeneric <string>(str);

            Assert.AreEqual(str, msg2, "Generic as String");

            // Generic as String, replacing the 'msg' variable from assertion 1 above
            msg = new MyGeneric <string>(str2);
            Assert.AreEqual(str2, msg, "Generic as String, replacing the 'msg' variable from assertion 1 above");

            // Generic as Int
            int int_instance2 = new MyGeneric <int>(int_base);

            Assert.AreEqual(int_base, int_instance2, "Generic as Int");

            // Generic as Float
            float float_gval = new MyGeneric <float>(float_base);

            Assert.AreEqual(float_base, float_gval, "Generic as Float");

            // Generic as Double
            double dbl_gval = new MyGeneric <double>(dbl_base);

            Assert.AreEqual(dbl_base, dbl_gval, "Generic as Double");
        }
示例#9
0
        static void Main(string[] args)
        {
            //var btn = new ButtonWithoutDelegate();
            //btn.Click();

            //var btn = new Button();
            //btn.OnButtonClick += Btn_OnButtonClick;

            //btn.Click("ConsoleApp", new BtnClickArgs { Location = "VCS" });



            var g = new MyGeneric <string>();
        }
        /// <summary>
        /// 反射调用泛型类
        /// </summary>
        public void TestGenericType()
        {
            #region 此时不对
            Assembly assembly = Assembly.LoadFrom("NineskyStudy.Base.dll");
            Type     type     = assembly.GetType("NineskyStudy.Base.MyGeneric`1"); //得到此类类型 注:(`1) 为占位符 不明确类型
            //在获取类型时,直接定义泛型类T,则下面调用方法中直接调用,不用设置类型T
            type = type.MakeGenericType(typeof(string));                           //指定泛型类
            object     obj = assembly.CreateInstance(type.FullName);               //Assembly.CreateInstance创建实例
            MethodInfo mi  = type.GetMethod("GetName");

            //调用泛型方法1
            string returnValue = (string)mi.Invoke(obj, new object[] { "123" });
            //调用泛型方法2
            string returnValue2 = (string)type.InvokeMember("GetName", BindingFlags.InvokeMethod, null, obj, new object[] { "123" });

            //检测是否是泛型 type.IsGenericType
            MyGeneric <System.String> genericObj = (MyGeneric <System.String>)Activator.CreateInstance(type);
            ////生成泛型方法
            MethodInfo m = genericObj.GetType().GetMethod("GetName");//.MakeGenericMethod(new Type[] { typeof(System.String) });
            ////调用泛型方法
            var value = m.Invoke(genericObj, new object[] { "a" });

            //获取类型
            Assembly assembly00 = Assembly.LoadFrom("NineskyStudy.Base.dll");
            Type     type00     = assembly00.GetType("NineskyStudy.Base.MyGeneric`1").MakeGenericType(typeof(string));
            Object   obj00      = assembly00.CreateInstance(type00.FullName);

            //当调用方法上又定义新的T1 需要设置新T1为具体类型 但InvokeMember没有找到(现在有错误)
            //type00.InvokeMember("GetName2", BindingFlags.InvokeMethod, null, obj00, new object[] { "a", "T1" });

            //当调用方法上又定义新的T1 需要设置新T1为具体类型
            MethodInfo m00     = obj00.GetType().GetMethod("GetName2").MakeGenericMethod(new Type[] { typeof(string) });
            string     value00 = (string)m00.Invoke(obj00, new object[] { "a", "T1" });
            #endregion

            //Type type = typeof(MyGeneric<string>);
            //object o = Activator.CreateInstance(type);
            //var result = type.InvokeMember("GetName", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, new object[] { "123" });

            //确定泛型参数类型反射方法
            Type   type1 = typeof(Class1 <int>);
            object o1    = Activator.CreateInstance(type1);
            type1.InvokeMember("Test", BindingFlags.Default | BindingFlags.InvokeMethod, null, o1, new object[] { 123 });

            //未确定泛型参数类型反射方法
            Type   type2 = typeof(Class1 <>).MakeGenericType(new Type[] { typeof(System.String) });
            object o2    = Activator.CreateInstance(type2);
            type2.InvokeMember("Test", BindingFlags.Default | BindingFlags.InvokeMethod, null, o2, new object[] { "123" });
        }
    public static void Main()
    {
        var myInt = new MyGeneric <int>();

        myInt.InstanceMethod();
        MyGeneric <int> .StaticMethod();

        MyGeneric <long> .StaticMethod();

        var myLong = new MyGeneric <long>();

        myLong.InstanceMethod();

        Console.ReadLine();
    }
        static void Main(string[] args)
        {
            var foot = new FooT();
              var foo = new Foo<FooT>();
              var foo2 = new Foo2<int>(); // prefered solution using CompareTo method
              var bar = new Bar<string>(); // Bar<T> has implementation eqivalent to Foo2<T>
              var myGen = new MyGeneric<cMyType>();

              Console.WriteLine(foo.IsInRange(foot));
              Console.WriteLine(foo2.IsInRange(5));
              Console.WriteLine(bar.IsInRange("nine"));
              Console.WriteLine(myGen + myGen);

              Console.WriteLine("Press ANY key to exit.");
              Console.ReadKey();
        }
示例#13
0
        public static void Main(string[] args)
        {
            Number <int> number = new Number <int>();

            number.number_one = 20;
            number.number_two = 122;
            number.total();

            number.genericMethod("param for generic method");

            Number <float> number2 = new Number <float>();

            number2.number_one = 12;
            number2.number_two = (float)3.14;
            MyGeneric <int, string> xyz = new MyGeneric <int, string>();
        }
示例#14
0
        static void Main(string[] args)
        {
            //GENERICS

            //A generic in C# is a type that can use objects of another type. We don't specify
            //the other type until the generic is used.

            //Class MyGeneric is defined earlier in this file.
            //Class OtherClass is defined in the OtherClasses.cs file
            var myGeneric      = new MyGeneric <int>();
            var myOtherGeneric = new MyGeneric <OtherClass>();

            //Right now, this isn't very interesting; MyGeneric doesn't actually do anything yet.
            //The most common use for generic types is creating special collection classes.
            //We're going to create one of those.
            //Head over to StackQueue.cs to continue.


            //RESUME
            Console.WriteLine("--------------------StackQueue<T>---------------------");
            //Because StackQueue is a generic class, we defer specifying the type
            //until an instance of it is created.
            var myStackQueue = new StackQueue <int>(); //T is now int

            myStackQueue.Enqueue(1);
            myStackQueue.Push(2);
            myStackQueue.Push(3);
            myStackQueue.Enqueue(4); //At this point, the collection is { 3, 2, 1, 4 }

            myStackQueue.Pop();      //3
            myStackQueue.Pop();      //2

            //We will get a build error if we attempt to push or enqueue
            //an object that is not of the type the StackQueue instance wants.

            //Uncomment the below line to get a compilation error.
            //myStackQueue.Push("a string");

            //GO TO SwapMethods.cs to continue this lesson.
            Console.WriteLine("--------------------Swapping---------------------");

            int a = 5;
            int b = 3;

            SwapMethods.Swap <int>(ref a, ref b);
            Console.WriteLine(a + " " + b); //Output: 3 5
        }
示例#15
0
        public void TestGenericParameter()
        {
            IDictionary <List <int>, List <string> > dico = new Dictionary <List <int>, List <string> >();

            Console.WriteLine(typeof(IDictionary <,>).FullName);
            Console.WriteLine(dico.GetType().FullName);

            string assemblyQualifiedName = dico.GetType().AssemblyQualifiedName;
            Type   type = TypeUtils.ResolveType(assemblyQualifiedName);

            Assert.IsNotNull(type);

            MyGeneric <Dictionary <List <int>, List <string> >, string, List <int>, decimal> gen = new MyGeneric <Dictionary <List <int>, List <string> >, string, List <int>, decimal>();

            Console.WriteLine(gen.GetType().FullName);

            assemblyQualifiedName = gen.GetType().AssemblyQualifiedName;
            type = TypeUtils.ResolveType(assemblyQualifiedName);

            Assert.IsNotNull(type);

            Assert.That(gen, Is.InstanceOfType(type));
        }
示例#16
0
 public void Generics()
 {
     MyGeneric.Test();
 }
示例#17
0
        static void Main(string[] args)
        {
            Console.WriteLine("-- ArrayList --");
            ArrayListDemo AlDemo = new ArrayListDemo();

            AlDemo.AddNew();
            AlDemo.Display();
            AlDemo.Sort();
            Console.WriteLine("Sorted List.");
            AlDemo.Display();

            Console.WriteLine("-- Stack --");
            StackDemo st = new StackDemo();

            QueueDemo Q = new QueueDemo();

            Console.WriteLine("-- Generic List --");
            MyGeneric <string> gen = new MyGeneric <string>();

            gen.AddItem("abc");
            gen.AddItem("xyz");
            Console.WriteLine(gen.GetItem());
            Console.WriteLine(gen.GetItem(1));

            Console.WriteLine("-- Exceptions --");
            //UserDefinedException uexp = new UserDefinedException();
            try
            {
                UserDefinedException.ThrowUserDefinedException();
            }
            catch (UserDefinedException ex)
            {
                Console.WriteLine("Exception : " + ex.Message);
            }
            try
            {
                UserDefinedException.ThrowUserDefinedException("New Message");
            }
            catch (UserDefinedException ex)
            {
                Console.WriteLine("Exception : " + ex.Message);
            }

            Console.WriteLine("\n-- Format Deemo --");
            FormattingDemo.Display();

            DisplayXMLContents();


            MyList list = new MyList();

            Console.WriteLine(list.ToString());

            Console.WriteLine("-- AbstractClass static methos--");
            Console.WriteLine(AbstractClass.Display("Hello"));

            Console.WriteLine("-- Yield Example --");
            foreach (int i in YieldExample.Power(2, 8))
            {
                Console.Write("{0} ", i);
            }

            //// Single Thread Example.
            Console.WriteLine("\n\nSingle Thread Example.\n");
            ThreadDemo thread1 = new ThreadDemo();
            ThreadDemo thread2 = new ThreadDemo("FromMain");

            Console.WriteLine("Single Thread Ended");

            //// Single Thread Example.
            Console.WriteLine("\n\nTwo Threads Example.\n");
            TwoThreads twoThreads = new TwoThreads("Child#1", "Child#2");

            Console.WriteLine("Two Threads Ended");
            Console.ReadKey();
        }
示例#18
0
 public static T Method2 <T>(this MyGeneric <T> generic)
 {
 }
示例#19
0
 static string CallGeneric <T>(MyGeneric <T, T> g)
 {
     return(g.NonVirtualMethod());
 }
示例#20
0
文件: main.cs 项目: bjjones/coreclr
 static void TestGenericVirtualMethod()
 {
     var o = new MyGeneric<String, Object>();
     Assert.AreEqual(o.GenericVirtualMethod<Program, IEnumerable<String>>(),
         "System.StringSystem.ObjectProgramSystem.Collections.Generic.IEnumerable`1[System.String]");
 }
示例#21
0
 public static T Method2(this MyGeneric <T> generic)
 {
     //does other good stuff with the generic..
 }