Hello All.
ForEverLearning:
Well, you don't want much, do you What makes any programming language a language is the rule structure of that language.
The properties you speak of reside in their respective class types, but you want to be able to access them from an instance of the base class instance object. C# has ways defined to do this, such as inheritance and polymorphism, but you don't want to use them.
You can use reflection to expose the properties of an unknown type at runtime in C#, but you don't want to use this either.
You could use a different method signature for each class type to access the particular properties, but you don't want to do that either.
Since a re-write of the C# compiler is probably out of the question, if you make some specific, concrete assumptions about your data (not generally considered a wise thing to do), then the following code might be of use:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Teacher tchr = new Teacher();
tchr.Name = "My Teacher Name";
tchr.BirthDate = "02/14/64";
object person = (object)tchr;
ListPersonInfo(person);
Student stdt = new Student();
stdt.Name = "My Student Name";
stdt.BirthDate = "04/28/28";
person = (object)stdt;
ListPersonInfo(person);
Console.ReadLine();
}
static void ListPersonInfo(object person)
{
if (person.ToString().EndsWith("Teacher"))
{
Teacher pers = (Teacher)person;
Console.WriteLine(pers.Name);
Console.WriteLine(pers.BirthDate);
}
if (person.ToString().EndsWith("Student"))
{
Student sPers = (Student)person;
Console.WriteLine(sPers.Name);
Console.WriteLine(sPers.BirthDate);
}
}
}
class Teacher
{
public string Name;
public string BirthDate;
}
class Student
{
public string Name;
public string BirthDate;
}
}
|