English 中文(简体)
参考物体的类型与背书储存的类型之间的区别
原标题:Distinguish between the type used to reference an object and the type of its backing store
using System;

interface IAnimal
{
}

class Cat: IAnimal
{
}

class Program
{
    public static void Main(string[] args)
    {
        IAnimal cat = new Cat();

        // Console.WriteLine(cat.GetType());
           // This would only give me the type of 
           // the backing store, i.e. Cat. Is there a 
           // way I can get to know that the identifier 
           // cat was declared as IAnimal?

        Console.ReadKey();
    }
}

Update: Thanks to Dan Bryant for the reminder.

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace TypeInfo
{
    class Program
    {
        public static void Main(string[] args)
        {
            IAnimal myCat = new Cat();
            ReflectOnType();
            Console.ReadKey();
        }

        public static void ReflectOnType()
        {
            Assembly.GetExecutingAssembly().
                GetType("TypeInfo.Program").
                GetMethod("Main", 
                BindingFlags.Static| BindingFlags.Public).
                GetMethodBody().LocalVariables.
                ToList().
                ForEach( l => Console.WriteLine(l.LocalType));
        }
    }

    interface IAnimal { }
    class Cat : IAnimal { }
}
问题回答

您可以采用通用的推论:

using System;

internal interface IAnimal
{
}

internal class Cat : IAnimal
{
}

class Program
{
    static void Main()
    {
        var cat = new Cat();
        Console.WriteLine(cat.GetType()); // Cat
        Console.WriteLine(GetStaticType(cat)); // Cat

        IAnimal animal = cat;
        Console.WriteLine(GetStaticType(animal)); // IAnimal
    }

    static Type GetStaticType<T>(T _)
    {
        return typeof (T);
    }
}

以上建议,我将此列为答案。 详见上文的评论意见。


您表示,你仍然看到有当地可变Info的后备仓库。 这对我来说意味着,声明纯粹是来源的,实际上根本不采用这种方法。 你选择使用一个接口,因为申报的类型并不重要,因为汇编者选择对当地变数位置使用更具体的类型。 掌握ILdasm的DL产出,你可以看到这是否正确。 如果是的话,你的唯一选择是实际研究源代码,因为所汇编的版本中确实有资料。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签