Thursday, July 29, 2010

How to create objects using string type name - C#

namespace ActiveTest
{
    public partial class Test1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Type type = Type.GetType("ActiveTest.Circle");
            IShape circle = Activator.CreateInstance(type) as IShape;
            ///
            /// you can cast your object to super level
            /// of if you have interface members you can 
            /// invoke them using interface members
            ///
            circle.Draw();
            circle.Paint();
 
            type = Type.GetType("ActiveTest.Squre");
            IShape squre = Activator.CreateInstance(type) as IShape;
            squre.Draw();
            squre.Paint();
        }
    }
    public interface  IShape
    {
        void Paint();
        void Draw();
    }
    public class Circle : IShape
    {
        #region IShape Members
 
        public void Paint()
        {
            ///
            /// Paint Circle 
            ///
        }
        public void Draw()
        {
            ///
            /// Draw Circle
            ///
        }
 
        #endregion
    }
    public class Squre : IShape
    {
        #region IShape Members
 
        public void Paint()
        {
            ///
            /// Paint squre
            ///
        }
        public void Draw()
        {
            ///
            /// Draw squre
            /// 
        }
 
        #endregion
    }
}

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...