如何在c#中的类中隐藏函数? [英] How to hide functions within classes in c#?

查看:440
本文介绍了如何在c#中的类中隐藏函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在类中隐藏几个方法基于传递给构造函数在c#中的参数。我该怎么做?
提前感谢!

I need to hide few methods inside a class based on the parameter that is passed to the constructor in c#. How would I do this? Thanks in advance!

更多信息:
我是这个GUI开发的一部分,他们有一个API可以访问硬件中的寄存器。现在他们正在发布一个更新的硬件,所以我需要支持旧的和新的一个更新的API(大部分重复的旧的一些旧的删除和一些新的寄存器添加)。

More Info: I was part of this GUI development where they had a API with access to registers in a hardware. Now they are releasing a newer hardware so I need to support both old and new one which has a newer API(Mostly duplicate of old one with some old removed and some new registers added).

此外,我需要保留这个只有一个名为API的类,因为我们在许多地方使用它。因此,排除使用不同名称的更新的API的想法。

Moreover, I need to keep this only one class called "API" as we used it in many places. So the idea of using a newer API with a different name was ruled out.

现在终于,我有了这样的想法:将新的API包含在有条件隐藏注册表访问方法。

Now finally, I got this idea of including the newer one into old one with just conditionally hiding the registry access methods.

推荐答案

你不能切换成员的可见性.....最好的办法是不同的接口隐藏成员。

You can't toggle the visibility of members..... the best bet is to have different interfaces that hide the members.

public interface IName
{
   string Name { get; set; }
}


public interface INumber
{
   string PhoneNumber { get; set; }
}


public class Worker : IName, INumber
{
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
}

因此,通过IName或INumber接口使用Worker,其他成员在类....

So either use Worker through the IName or the INumber interface and it will hide the other members on the class....

这篇关于如何在c#中的类中隐藏函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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