简体   繁体   English

为什么对象在上传时会保存属性

[英]Why an object saves properties when upcasted

I've got a simple question concerning upcasting in c#. 关于c#中的upcast,我有一个简单的问题。 For example I have two interfaces: 例如,我有两个接口:

interface IUSer
{
    string UserName { get; set; }
}

interface IAdmin: IUSer
{
    string Password { get; set; }
}

...and a class implementing IAdmin interface: ...以及实现IAdmin接口的类:

class Admin : IAdmin
{
    public string Password { get; set; }
    public string UserName { get; set; }
}

When I create an object of type Admin and try to upcast it to IUser, technically I can then only access UserName user.UserName: 当我创建一个Admin类型的对象并尝试将其向上转换为IUser时,从技术上讲,我只能访问UserName user.UserName:

var admin = new Admin() { UserName = "Arthur", Password = "hello" };
var user = (IUSer)admin;
Console.WriteLine( user.UserName );

...but If I cycle through this object's properties using Reflection, like that: ...但是如果我使用Reflection循环遍历此对象的属性,就像这样:

    private static void Outputter(IUSer user)
    {
        foreach (var prop in user.GetType().GetProperties())
        {
            Console.WriteLine(prop.Name +  " " + prop.GetValue(admin));
        }
    }

...I can also see Password property. ...我也可以看到密码属性。 The question is - why it is saved when upcasted? 问题是 - 为何在升级时保存?

The question is - why it is saved when upcasted? 问题是 - 为何在升级时保存?

Because casting doesn't change the object at all. 因为铸造根本不会改变对象 It just changes how you're viewing the object. 它只会改变您查看对象的方式。

Think of it like being a person. 把它想象成一个人。 You may have different ways that people can view you: 您可能有不同的方式让人们可以看到您:

  • A colleague 一个同事
  • A manager 经理
  • A family member 一位家庭成员
  • A friend 一个朋友

They only "see" certain aspects of you, but that doesn't change who you are. 他们只“看到”你的某些方面,但这不会改变你的身份。

Likewise a simple reference conversion doesn't change the object. 同样,简单的引用转换不会更改对象。 If you call user.GetType() it will still report the actual type of the object. 如果你调用user.GetType()它仍然会报告对象的实际类型。 It's just that when you view the object through a particular lens (whether that's an interface or a base class) you only see the members that lens shows you. 只是当您通过特定镜头(无论是界面还是基类)查看对象时,您只能看到镜头向您显示的成员。

(Note that a cast that calls a user-defined conversion, eg casting from XElement to string , is entirely different. That returns a reference to a new object, rather than just regarding the existing object in a different way.) (请注意,调用用户定义的转换的转换,例如从XElement转换为string ,完全不同。它返回对新对象的引用,而不是仅以不同的方式关注现有对象。)

If you only want to see the IUser properties, use typeof(IUser).GetProperties() instead of calling GetType() first: 如果您只想查看IUser属性,请使用typeof(IUser).GetProperties()而不是先调用GetType()

private static void Outputter(IUser user)
{
    foreach (var prop in typeof(IUser).GetProperties())
    {
        Console.WriteLine($"{prop.Name}: {prop.GetValue(user)}");
    }
}

Although I'm not sure it's particularly useful to do that with reflection in most cases. 虽然我不确定在大多数情况下用反射来做这件事特别有用。

when you cast the object of any type to their base type, you are just holding that object with they type of base. 当你将任何类型的对象强制转换为它们的基类型时,你只需要保持那个对象类型为base的对象。

object doesn't loos its properties by that. 对象不会因此而忽略其属性。

If you see a simplest example : Use of non generic collection 如果您看到一个最简单的示例: 使用非泛型集合

when you add some object (let's say of Admin class) in ArrayList , you are actually converting that object into type object (Boxing) and then store it arraylist. 当您在ArrayList添加一些对象(比如Admin类)时,您实际上是将该对象转换为类型object (Boxing),然后将其存储为arraylist。 So it is ultimate example of up-casting! 所以这是上传的最终例子! How? 怎么样?

The object type is an alias for Object in the .NET Framework. 对象类型是.NET Framework中Object的别名。 In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. 在C#的统一类型系统中,所有类型,预定义和用户定义的引用类型和值类型都直接或间接地从Object继承。

Full Post 全职

Now object type doesn't hold properties like userName password . 现在, object类型不包含userName password等属性。 So when you try to access those property from the object you get directly from arraylist, you will not able to do so. 因此,当您尝试从arraylist直接获得的对象访问这些属性时,您将无法执行此操作。 Because current you are dealing with the type object and you will get access only those properties or members which is supported by object type (but here you are not getting access, still those properties are there). 因为当前您正在处理类型object并且您只能访问object类型支持的那些属性或成员(但是在这里您无法访问,但仍然存在这些属性)。 In addition as note in other answer GetType() method will surely return it's original type too. 另外作为其他答案中的注释GetType()方法也一定会返回它的原始类型。

But when you get that original object back from array list by casting it back to Admin type (Unboxing) , you will see those properties are there in your object as they were before. 但是,当您将该原始对象从阵列列表中返回到Admin类型(取消装箱)时,您将看到这些属性在您的对象中,就像之前一样。 Only thing which is changed is, now you are able to access them because Admin type supports them. 只有更改的是,现在您可以访问它们,因为Admin类型支持它们。

Read this for more detail 阅读本文了解更多详情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DataContractSerializer,为什么“ this”没有被转换? - DataContractSerializer, why is “this” not upcasted? 为什么将位图保存为PNG - Why Bitmap saves to PNG 是否可以再次下载一个上升的对象而不为基类类型的每个派生类类型尝试强制转换? - Can an upcasted object be downcasted again without trying a cast for every derived class type of the base class type? 当我们在 Linq 上使用 .Select(x=>new object) 时,为什么我们不能在多方面选择任何属性 - Why cant we select any properties on the many side when we are using .Select(x=>new object) on Linq 无法访问的上报字段 - UpCasted field without access 为什么当我的 IIS 工作人员在 ASP.NET 中保存文件时权限不稳定? - Why are permissions wonky when my IIS worker saves a file in ASP.NET? 为什么我的自定义 XML 在 Word 保存 DOCX 文件时不会延续到新版本? - Why does my custom XML not carry over to a new version of a DOCX file when Word saves it? 为什么 Connection Object 中包含 Null 属性 - Why does Connection Object contain Null properties in it 设计帮助 - 对象修改并保存另一个对象 - Design Help - Object modifies and saves another Object 为什么在向自定义控件添加新属性时不显示属性? - Why when adding new properties to custom control the properties not show?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM