Bodyless constructor in non-abstract C# class

自闭症网瘾萝莉.ら 提交于 2019-12-18 09:02:24

问题


I'm trying to understand the following class provided in the MVC framework; it seems like the class should be abstract, but it is not, and yet this class compiles. Is the class actually abstract despite the missing "abstract" keyword? What am I missing here?

namespace Microsoft.AspNet.Identity.EntityFramework
{
    public class IdentityUser : IUser
    {
        public IdentityUser();
        public IdentityUser(string userName);

        public virtual ICollection<IdentityUserClaim> Claims { get; }
        public virtual string Id { get; set; }
        public virtual ICollection<IdentityUserLogin> Logins { get; }
        public virtual string PasswordHash { get; set; }
        public virtual ICollection<IdentityUserRole> Roles { get; }
        public virtual string SecurityStamp { get; set; }
        public virtual string UserName { get; set; }
    }
}

Edit:

Lesson learned: if there is no source provided for a binary, using "Go To Definition" in Visual Studio will provide something that looks mostly like source code... which is different than using "Open Declaration" in Eclipse, which will show you something that really doesn't look like source.


回答1:


This is a Microsoft's class which I presume you retrieved the code for by "Go to Definition" context menu in Visual Studio. This code is auto generated by reflecting over the type, and only is meant to show declarations and not the implementation content. If you use .NET Refector or dotReflect tools, then you'll see body of the implementation.




回答2:


What you are missing is that you are not actually looking at the source code for the class. It is part of ASP.NET, documented here. The source code is kept in Redmond Washington, out of reach of your machine and not part of the .NET Reference Source.

What you got is an auto-generated document, produced from the metadata in the Microsoft.AspNet.Identity.EntityFramework.dll assembly. Which only contains the class declaration, the code was compiled into MSIL by the C# compiler. You'd need a good decompiler to see a resemblance of that code, Reflector and ILSpy are popular.

Be careful when you use Go To Definition in Visual Studio. You only see real source code when the IDE can find it.



来源:https://stackoverflow.com/questions/22409520/bodyless-constructor-in-non-abstract-c-sharp-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!