3
Reply

Decide to instantiate classes at run time with different pro

Upkar Srivastava

Upkar Srivastava

Jul 18 2014 2:00 PM
784
nterface InterfaceToimplement
{
string Name { get; set; }
}

Implementing Classes
class Student:InterfaceToimplement
{
public string Name { get; set; }
public int ID { get; set; }
public int RollNumber { get; set; }
}

class Room : InterfaceToimplement
{
public string Name { get; set; }
public int Length { get; set; }
public int Breadth { get; set; }
public int Height { get; set; }
}

class Account : InterfaceToimplement
{
public string Name { get; set; }
public string AccountholderName { get; set; }
public int AccountID { get; set; }
public int Amount { get; set; }
}


Factory Class
class ClassFactory
{
public InterfaceToimplement GetClass(string classname)
{
InterfaceToimplement ObjectClass = null;
switch (classname)
{
case "Student":
ObjectClass = new Student();
break;
default:
break;
}

return ObjectClass;
}
}

Calling Method
static void Main(string[] args)
{
string classname = "Student";
ClassFactory clsfactory = new ClassFactory();
InterfaceToimplement GetObjIntrface = null;
GetObjIntrface = clsfactory.GetClass(classname);

}

When I use GetObjIntrface. Then i use only properties of interface but i want all the properties of class Student. Suggest right approch

Answers (3)