[You must know. NET] 34th back, object member, no more!

Source: Internet
Author: User
Document directory
  • References

Anytao.net |. NET website you must know|Anytao technology blog

Released on: 2009.10.30 by: Anytao
2009 original works of Anytao.com and Anytao. Please refer to the author and source in the post.

 

In the. NET world, objects are recognized as creators. the seven largest members of the object are rampant in any code corner of any system.

public class Object{    public Object();    public virtual bool Equals(object obj);    public static bool Equals(object objA, object objB);    public virtual int GetHashCode();    public Type GetType();    protected object MemberwiseClone();    public static bool ReferenceEquals(object objA, object objB);    public virtual string ToString();}

There are many stories about objects. Today's story focuses on "doing unfamiliar things for familiar friends ". Believe it? My Object member disappears. If you believe it, you can appreciate the member.

Haha! It's more fresh than the following common encoding methods:

Is it confusing. Generally speaking, at any time, in the long member method list, you can always see them. Who makes the object the base class of everything? However, in some cases, I may want to keep the root of the eye clean and block the unused parent class members, making method calls more concise, just like the IObject member above.

How can we achieve this? Although I am an honest person, this is actually a naked scam, and the scam guy is coming soon:

namespace System.ComponentModel{    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate)]    public sealed class EditorBrowsableAttribute : Attribute    {    }}

Yes, it is exactly System. CompentModel. EditorBrowsableAttriute. For the above example, I actually implemented the following encoding for the AnyObject class:

public interface IObject : IAnyObject{    void About();}

 

public class AnyObject : IObject{    public void About()    {    }}

The core is the definition of IAnyObject:

namespace Anytao.Core.Common{    /// <summary>    /// A common interface for any object    /// </summary>    [EditorBrowsable(EditorBrowsableState.Never)]    public interface IAnyObject    {        [EditorBrowsable(EditorBrowsableState.Never)]        bool Equals(object obj);        [EditorBrowsable(EditorBrowsableState.Never)]        int GetHashCode();        [EditorBrowsable(EditorBrowsableState.Never)]        Type GetType();        [EditorBrowsable(EditorBrowsableState.Never)]        string ToString();    }}

In general, my Anytao. core. A Common IAnyObject interface is provided in the basic components of Common. The function of this interface is to magically hide the object members, just like the IObject in the beginning of this article.

Therefore, when we return to the System. CompentModel. EditorBrowsableAttriute feature, we can see that it is used to identify the visibility of a class or attribute in the editor. So, are they actually gone? Continue to apply the initial code:
class Program{    static void Main(string[] args)    {        IObject obj = new AnyObject();        Console.WriteLine(obj.ToString());    }}

We found that although ToString is invisible to obj, there is still no problem with calling during runtime. Therefore, in general, System. componentModel. editorBrowsableAttribute is only a blind-eye method, which controls Visual Studio's smart perception. The EditorBrowsableState option mainly includes:

  • Advanced: You can use Visual Studio's Options-> Text Editor-> C #-> General's Hide advanced members settings in the same way for Advanced member Options.
  • Always, Always visible.
  • Never, always invisible.

Note: in Visual C #, EditorBrowsableAttribute is not valid for members of the same assembly.

 

What are you waiting? Try it too? In addition, there are many interesting features worth exploring about the editor and the power of your discovery.

 

References
  • Show Advanced Members in Visual Studio, http://www.blackwasp.co.uk/ShowAdvancedMembers.aspx

 

For more gossip, follow anytao.net

 

Anytao | 2009 Anytao.com

2009/10/30 | http://anytao.cnblogs.com/| http://www.anytao.net/blog/post/2009/10/29/anytao-insidenet-34-object-members-hide.aspx

This document is provided as "the status quo" without any guarantee and does not grant any rights. | This posting is provided "as is" with no warranties, and confers no rights.

The copyright of this article is owned by the author. You are welcome to repost this article, but you must keep this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be entitled to pursue legal liability.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.