我可以隐藏.ToString()方法 [英] Can i Hide .ToString() Method

查看:92
本文介绍了我可以隐藏.ToString()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个类,当我创建该类的对象时,我希望obj.ToString()不会显示并且无法使用此方法

解决方案

< blockquote>我不确定你可以隐藏它,因为ToString是一个虚方法。如果它不是,你可以在你的班级中新建/覆盖它作为一个私有方法,它会被隐藏,但你显然不允许用虚拟方法做到这一点。


然而,你可以覆盖它以使它什么都不做,这是你想要的一部分。


我通过谷歌进行了一些挖掘并且有一些建议,但是当我尝试它们时它们并没有工作。 ToString方法仍然出现在intellisense上,仍然可以访问。你为什么要隐藏它呢?


你可以把一些关键字放在[]括号内,然后在一个函数中隐藏它以防止它被智能感知,不确定它是否适用于基本的必需.ToString()函数


我前几天正在与某人谈论他们的问题,使用反射方法标记为......某事......使得该方法隐形。


正如Plater所说,你可以使用[]中指定的某些属性来表明这一点。在方法之前......我只是不记得属性的名称。


您是否尝试在覆盖方法时将Public范围声明更改为Private ?

-Frinny


I want to create a class and when ever i create an object of that class i want that obj.ToString() will not display and cant use this method

解决方案

I''m not sure you can hide it because ToString is a virtual method. If it weren''t, you could just new/override it as a private method in your class and it would be hidden, but you''re apparently not allowed to do that with virtual methods.

However, you can override it to make it do nothing at all, which is part of what you want.

I did some digging around via google and there were some suggestions, but they didn''t work when I tried them. The ToString method still showed up on intellisense and was still accessible. Why do you need it to be hidden?


There is some sort of keyword you can put inside [ ] brackets before a function to hide it from intelisense, not sure it will work with the basic required .ToString() function


I was just having a conversation with someone the other day about their problems using reflection with a method that was marked as..."something"...which made the method "invisible".

As Plater said, You can indicate this using some attribute that is specified in "[]" that comes before the method....I just can''t remember the name of the attribute.

Have you tried just changing the Public scope declaration to Private when you override the method?

-Frinny


这篇关于我可以隐藏.ToString()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆