예제 #1
0
        public static void Main()
        {
            MyBaseClass    myBase    = new MyBaseClass();
            MyDerivedClass myDerived = new MyDerivedClass();

            object o = myDerived;
            //MyBaseClass b = myDerived;
            var b = myDerived;

            Console.WriteLine("myBase: Type is {0}", myBase.GetType());
            Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
            Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
            Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());

            Console.WriteLine("-----------------------------------------");

            int  n1 = 12;
            int  n2 = 82;
            long n3 = 12;

            Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType()));

            Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType()));

            Console.WriteLine("-----------------------------------------");

            object[] values = { (int)12, (long)10653, (byte)12, (sbyte)-5, 16.3, "string" };

            foreach (var value in values)
            {
                Type t = value.GetType();

                if (t.Equals(typeof(byte)))
                {
                    Console.WriteLine("The value: {0} is an unsigned byte.", value);
                }
                else if (t.Equals(typeof(sbyte)))
                {
                    Console.WriteLine("The value: {0} is a signed byte.", value);
                }
                else if (t.Equals(typeof(int)))
                {
                    Console.WriteLine("The value: {0} is a 32-bit integer.", value);
                }
                else if (t.Equals(typeof(long)))
                {
                    Console.WriteLine("The value: {0} is a 32-bit integer.", value);
                }
                else if (t.Equals(typeof(double)))
                {
                    Console.WriteLine("The value: {0} is a double-precision floating point.", value);
                }
                else
                {
                    Console.WriteLine("The value: '{0}' is another data type.", value);
                }
            }
        }
예제 #2
0
    public static void Main()
    {
        MyBaseClass    myBase    = new MyBaseClass();
        MyDerivedClass myDerived = new MyDerivedClass();
        object         o         = myDerived;
        MyBaseClass    b         = myDerived;

        Console.WriteLine("mybase: Type is {0}", myBase.GetType());
        Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
        Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
        Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
    }
예제 #3
0
    public static void Retrieve(this MyBaseClass entity, int ID)
    {
        string    className = entity.GetType().Name;
        Database  db        = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = db.GetStoredProcCommand(String.Format("{0}s_Retrieve_{0}", className));

        db.AddInParameter(dbCommand, String.Format("@{0}ID", className), DbType.Int32, ID);
        using (IDataReader dr = db.ExecuteReader(dbCommand))
        {
            if (dr.Read())
            {
                BOLoader.LoadDataToProps(this, dr);
            }
        }
    }
예제 #4
0
    public override int CompareTo(MyBaseClass other)
    {
        if (other == null)
        {
            return(1);
        }

        var typedOther = other as MyBaseClass;

        if (typedOther != null)
        {
            return(this.SomeValue.CompareTo(typedOther.SomeValue));
        }
        else
        {
            return(GetType().FullName.CompareTo(other.GetType().FullName));
        }
    }
    public override int CompareTo(MyBaseClass other)
    {
        if (other == null)
        {
            // every instance comes after null, cf. docs
            return(1);
        }

        var typedOther = other as MyBaseClass;

        if (typedOther != null)
        {
            // other instance of same type; compare by custom sorting criteria
            return(this.SomeValue.CompareTo(typedOther.SomeValue));
        }
        else
        {
            // other instance of different type; make sure different types are always sorted in the same order
            return(GetType().FullName.CompareTo(other.GetType().FullName));
        }
    }
예제 #6
0
        static void Main(string[] args)
        {
            MyBaseClass    myBase    = new MyBaseClass();
            MyDerivedClass myDerived = new MyDerivedClass();
            object         o         = myDerived;
            MyBaseClass    b         = myDerived;

            Console.WriteLine("mybase: Type is {0}", myBase.GetType());
            Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
            Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
            // Class Type不會因此被覆蓋
            Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());

            int  n1 = 12;
            int  n2 = 82;
            long n3 = 12;

            Console.WriteLine("n1 and n2 are the same type: {0}",
                              Object.ReferenceEquals(n1.GetType(), n2.GetType()));
            Console.WriteLine("n1 and n3 are the same type: {0}",
                              Object.ReferenceEquals(n1.GetType(), n3.GetType()));

            Console.ReadLine();
        }