Click here to Skip to main content
15,914,066 members
Home / Discussions / C#
   

C#

 
GeneralRe: delegates and events Pin
staticv24-Jan-09 5:01
staticv24-Jan-09 5:01 
GeneralRe: delegates and events Pin
jkohler24-Jan-09 4:51
jkohler24-Jan-09 4:51 
AnswerRe: delegates and events [modified] Pin
Luc Pattyn24-Jan-09 5:09
sitebuilderLuc Pattyn24-Jan-09 5:09 
GeneralRe: delegates and events Pin
jkohler24-Jan-09 6:48
jkohler24-Jan-09 6:48 
AnswerRe: delegates and events Pin
DaveyM6924-Jan-09 4:13
professionalDaveyM6924-Jan-09 4:13 
GeneralRe: delegates and events Pin
Luc Pattyn24-Jan-09 4:54
sitebuilderLuc Pattyn24-Jan-09 4:54 
AnswerRe: delegates and events Pin
Giorgi Dalakishvili24-Jan-09 4:41
mentorGiorgi Dalakishvili24-Jan-09 4:41 
QuestionUsing A Retrieved Type Pin
#realJSOP24-Jan-09 2:24
professional#realJSOP24-Jan-09 2:24 
Given the following code:

public class Employee1 
{
    public string Property1 { get; set; }
    public Employee1() {}
}

public class Employee2:Employee1 
{
    public string Property2 { get; set; }
    public Employee2():base() {}
}

public class Form1
{
    List<employee1> m_employees = new List<employee1>();

    private void Add()
    {
        m_employees.Add(new Employee1());
        m_employees.Add(new Employee2());
    }

    private bool IsType(object item, string typeName)
    {
        bool result = false;
        // Make sure the object isn't null AND that the string isn't null or empty.
        if (item != null && !string.IsNullOrEmpty(typeName))
        {
            // Get the type of the item being checked
            result = (item.GetType().Name == typeName);
        }
        return result;
    }
}


I want to add the items in this list to a ListView, but I have to cast the objects to their actual types, like so:

string[] listItems = new string[2];
if (IsType(obj, "Employee2"))
{
    listItems[0] = ((Employee2)obj).Property1;
    listItems[1] = ((Employee2)obj).Property2;
}
else
{
    listItems[0] = ((Employee1)obj).Property1;
    listItems[1] = "Not applicable";
}


I want something more generic (I want to avoid using the actual type names if possible). I tried to use various properties in the type object returned by Type.GetType("Employee2", true, false); as a casting mechanism, but nothing seems to work.

I want to do something like this:

string[] listItems = new string[2];

Type itemType = obj.GetType();
listItems[0] = (itemType)obj).Property1;
if (IsType(obj, "Employee2"))
{
    listItems[1] = ((itemType)obj).Property2;
}
else
{
    listItems[1] = "Not applicable";
}



Does anyone have any ideas?


"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001


AnswerRe: Using A Retrieved Type Pin
User 665824-Jan-09 2:27
User 665824-Jan-09 2:27 
GeneralRe: Using A Retrieved Type Pin
#realJSOP24-Jan-09 2:28
professional#realJSOP24-Jan-09 2:28 
GeneralRe: Using A Retrieved Type Pin
User 665824-Jan-09 2:36
User 665824-Jan-09 2:36 
AnswerRe: Using A Retrieved Type Pin
DaveyM6924-Jan-09 2:35
professionalDaveyM6924-Jan-09 2:35 
GeneralRe: Using A Retrieved Type Pin
User 665824-Jan-09 2:39
User 665824-Jan-09 2:39 
GeneralRe: Using A Retrieved Type Pin
DaveyM6924-Jan-09 2:48
professionalDaveyM6924-Jan-09 2:48 
GeneralRe: Using A Retrieved Type Pin
#realJSOP24-Jan-09 3:10
professional#realJSOP24-Jan-09 3:10 
GeneralRe: Using A Retrieved Type Pin
#realJSOP24-Jan-09 3:13
professional#realJSOP24-Jan-09 3:13 
AnswerRe: Using A Retrieved Type Pin
DaveyM6925-Jan-09 0:42
professionalDaveyM6925-Jan-09 0:42 
GeneralRe: Using A Retrieved Type Pin
#realJSOP26-Jan-09 2:56
professional#realJSOP26-Jan-09 2:56 
GeneralRe: Using A Retrieved Type Pin
DaveyM6926-Jan-09 3:50
professionalDaveyM6926-Jan-09 3:50 
GeneralRe: Using A Retrieved Type Pin
#realJSOP26-Jan-09 5:28
professional#realJSOP26-Jan-09 5:28 
GeneralRe: Using A Retrieved Type Pin
DaveyM6926-Jan-09 10:02
professionalDaveyM6926-Jan-09 10:02 
GeneralRe: Using A Retrieved Type Pin
DaveyM6926-Jan-09 4:11
professionalDaveyM6926-Jan-09 4:11 
AnswerRe: Using A Retrieved Type Pin
DaveyM6926-Jan-09 11:26
professionalDaveyM6926-Jan-09 11:26 
Questionİf one array Label Enabled=false Pin
ammoti24-Jan-09 1:51
ammoti24-Jan-09 1:51 
GeneralRe: İf one array Label Enabled=false Pin
Guffa24-Jan-09 9:12
Guffa24-Jan-09 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.