locked
Runtime casting of an object type to access it's properties (C#) RRS feed

  • Question

  • How can I cast an object type at runtime?

    Suppose I have a method GetProperties() that has one parameter of type object. I pass either a parameter (of type ParameterInfo) or a property (of type PropertyInfo) to this method. I want to get the name of the passed parameter/property. In the below code, I am able to get the type of the object passed. Now I am stuck on how to type cast the object to get the name of the parameter?  

    public void GetProperties(object obj)
    {
      Type t = obj.GetType();
      string name = ((t)obj).Name;
    }
    

     

     

     

    Monday, January 24, 2011 5:42 PM

Answers

  • If you know those are the only two types being passed, I'd recommend overloading the method:

    public void GetProperties(ParameterInfo parameter)
    {
     string name = parameter.Name;
      // Use name...
    }
    
    public void GetProperties(PropertyInfo propertyInfo)
    {
      string name = propertyInfo.Name;
      // Use Name...
    }
    

    If you must leave a single method using object, you can do:

     

    public void GetProperties(object obj)
    {
     string name = string.Empty; 
    
     PropertyInfo propInfo = obj as PropertyInfo
     ParameterInfo paramInfo = obj as ParameterInfo;
    
     if (propInfo != null)
       name = propInfo.Name;
     else if (paramInfo != null)
       name = paramInfo.Name;
     else
       throw new ArgumentException("This method only accepts PropertyInfo and ParameterInfo arguments");
     
      // Use name...
    }
     
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, January 24, 2011 6:51 PM
    Moderator

All replies

  • If you know those are the only two types being passed, I'd recommend overloading the method:

    public void GetProperties(ParameterInfo parameter)
    {
     string name = parameter.Name;
      // Use name...
    }
    
    public void GetProperties(PropertyInfo propertyInfo)
    {
      string name = propertyInfo.Name;
      // Use Name...
    }
    

    If you must leave a single method using object, you can do:

     

    public void GetProperties(object obj)
    {
     string name = string.Empty; 
    
     PropertyInfo propInfo = obj as PropertyInfo
     ParameterInfo paramInfo = obj as ParameterInfo;
    
     if (propInfo != null)
       name = propInfo.Name;
     else if (paramInfo != null)
       name = paramInfo.Name;
     else
       throw new ArgumentException("This method only accepts PropertyInfo and ParameterInfo arguments");
     
      // Use name...
    }
     
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, January 24, 2011 6:51 PM
    Moderator
  • Thanks Reed for your response. I am trying to make one generic method that can handle property and parameter both. I was planning to pass a parameter of type ParameterInfo and within call the function recursively for the properties for the passed parameter. That was the reason why I want to have a code that will work for any type (parameter/property). If I go with the second approach that you mentioned, then I need to have the logic repeated for both property and parameter types. Is it?

    Also, can you tell me how can I get the name of the class if the passed value is an object of ClassA? In obj, I have the elements/properties of ClassA and that can be accessed using

    foreach (PropertyInfo prop in obj.GetType().GetProperties())
    

    But I don't know how to access class level information.. like Class Name should give me ClassA?

    Monday, January 24, 2011 7:40 PM
  • You can use obj.GetType() to get the System.Type for the object. Given the Type, you can use Type.Name (or Type.FullName) to retrieve the type's name:

     

        string objectTypeName = obj.GetType().Name;

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, January 24, 2011 7:45 PM
    Moderator