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

前端 未结 12 1270
暗喜
暗喜 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:48

    It's possible if you don't mind declaring your method as a generic. Try this.

    class Program
    {
        public static void GetParameterValue(out T destination)
        {
            Console.WriteLine("typeof(T)=" + typeof(T).Name);
            destination = default(T);
        }
        static void Main(string[] args)
        {
            string s;
            GetParameterValue(out s);
            int i;
            GetParameterValue(out i);
        }
    }
    

提交回复
热议问题