.NET : How do you get the Type of a null object?

前端 未结 12 1283
暗喜
暗喜 2020-11-28 13:59

I have a method with an out parameter that tries to do a type conversion. Basically:

public void GetParameterValue(out object destination)
{
    object param         


        
12条回答
  •  半阙折子戏
    2020-11-28 14:40

    http://msdn.microsoft.com/en-us/library/58918ffs.aspx

    or

    private Hashtable propertyTable = new Hashtable();
    
    public void LoadPropertyTypes()
    {
        Type t = this.GetType();
    
        System.Reflection.MemberInfo[] memberInfo = t.GetMembers();
    
        foreach (System.Reflection.MemberInfo mInfo in memberInfo)
        {
            string[] prop = mInfo.ToString().Split(Convert.ToChar(" "));
            propertyTable.Add(prop[1], prop[0]);
        }
    }
    public string GetMemberType(string propName)
    {
        if (propertyTable.ContainsKey(propName))
        {
            return Convert.ToString(propertyTable[propName]);
        }
        else{
            return "N/A";
        }
    }
    

    in that way we can use switch to manage different property types.

提交回复
热议问题