using System;

class aa
{
}
class bb : aa
{
}
class cc : bb
{

}


Can you create an instance of class aa from
the instance of class cc?

 Posted by Ddd on 2/15/2011 | Category: C# Interview questions | Views: 2829 | Points: 40
Answer:

Yes, we can create an instance of class aa from
the instance of class cc

add this code snippet
(after the end of the definition of class cc)
class result

{
static void Main(string[] args)
{
cc obj = new cc();
//get the base type of cc

Type b = obj.GetType().BaseType;
//It will be bb
Console.WriteLine(b.Name);

// again get parent for bb type, which is aatype
Type a = b.BaseType;
// create a instance usng Activator.CreateInstance
object o = Activator.CreateInstance(a);


Console.WriteLine(o.GetType().Name);
//It will be aa


}
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response