B
Brian Pelton
I want to use reflection to do some special logic on properties of a
specific type (or those that inherit from that type)
Below is an example of what I would like to do. However, I get a
complier error saying that the property will never be of the provided
type. I don't understand.
How can I do this???
Thanks for any help,
Brian
using System;
using System.Reflection;
namespace ReflectSandbox
{
class Employee
{
public FullName EmployeeName;
public string Address;
public string PhoneNumber;
}
class Name
{
public string FirstName;
public string LastName;
}
class FullName : Name
{
public string MiddleName;
}
class Program
{
static void Main(string[] args)
{
Employee a = new Employee();
a.EmployeeName.FirstName = "John";
a.EmployeeName.MiddleName = "Jacob";
a.EmployeeName.LastName = "Smith";
a.Address = "101 N Anywhere";
a.PhoneNumber = "1-800-HELP-ME";
foreach (PropertyInfo p in a.GetType().GetProperties())
{
if (p.PropertyType is Name) //Given Expression is never
//of the provided type
{
//here, do special logic
//for properties of Name type
//or any type that inherits from
//name (i.e. FullName)
}
}
}
}
}
specific type (or those that inherit from that type)
Below is an example of what I would like to do. However, I get a
complier error saying that the property will never be of the provided
type. I don't understand.
How can I do this???
Thanks for any help,
Brian
using System;
using System.Reflection;
namespace ReflectSandbox
{
class Employee
{
public FullName EmployeeName;
public string Address;
public string PhoneNumber;
}
class Name
{
public string FirstName;
public string LastName;
}
class FullName : Name
{
public string MiddleName;
}
class Program
{
static void Main(string[] args)
{
Employee a = new Employee();
a.EmployeeName.FirstName = "John";
a.EmployeeName.MiddleName = "Jacob";
a.EmployeeName.LastName = "Smith";
a.Address = "101 N Anywhere";
a.PhoneNumber = "1-800-HELP-ME";
foreach (PropertyInfo p in a.GetType().GetProperties())
{
if (p.PropertyType is Name) //Given Expression is never
//of the provided type
{
//here, do special logic
//for properties of Name type
//or any type that inherits from
//name (i.e. FullName)
}
}
}
}
}