40327

How to hide functions within classes in c#?

Question:

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!

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).

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.

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

Answer1:

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; } }

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

Answer2:

I think you might be looking at a re-factor here. Try making a base class with all the methods / properties that behave the same regardless of the parameter, then two child classes which behave differently. Also have a look at the class factory pattern.

Answer3:

You will need to restructure the code into multiple classes or interfaces. You can't dynamically change the visibility level of class members based on a parameter value. Members are construction time information not run time.

Answer4:

You can't change visibility of methods, but you can pass parameter to the single method and do some logic depending on your parameter by using switch - case. But it depends on your method structure. <br /> Try to review your class and change its design. Maybe you can find any design pattern that will help you.

Answer5:

You may actually do something alike using <a href="http://www.devx.com/dotnet/Article/28783/1954" rel="nofollow">Dynamic code generation</a>, although this is more like a hack than actual code that should be used in production.

Maybe if you explain <em>why</em> you need this then you may get more relevant answers.

Recommend

  • XSL - Duplicate child's parent node
  • QLPreviewController: quicklookd failing to load
  • C++: why constructor “A(A a) {}” is illegal? [duplicate]
  • MVC3 - Conditionally add id element with Razor
  • Jquery javascript: have a filtering list(works), need help keeping all LI's visible when there&
  • Magento - Getting the Details of Multiple Products in a Single XML-RPC call
  • How to hide the cursor in windows when dragging and dropping (possibly in python, or another languag
  • Can I put condition in emacs lisp macro?
  • Application Express: Anonymous PL/SQL Block and Bind Variables
  • Calling a second level base class constructor
  • How to prevent two volume claims to claim the same volume on Kubernetes?
  • How to restrict number of concurrent processes?
  • does the FROM directive in a dockerfile allways pull the latest version of an image
  • Selenium and Google - How do you use cookies?
  • disablinging autorecover option for powepoint
  • When querying against a view, a filtering clause in the view's definition is being ignored
  • error in docker build publish plugin
  • Jquery Show & ScrollTop (or ScrollTo)
  • CSS: How to fix overlapping divs
  • Use neo4j server instead of embedded mode
  • Change navbar in bootstrap if user login
  • custom string delimiters stringtemplate-4
  • Android Chronometer starts and stops but carries on counting when stopped
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Suppressing passwd when calling sqlplus from shell script
  • How to remove a SwiftyJSON element?
  • Jackson Parser: ignore deserializing for type mismatch
  • Android full screen on only one activity?
  • jQuery .attr() and value
  • Read text file and split every line in MSBuild
  • Azure Cloud Service Web Role web pages do not load
  • C# - Serializing and deserializing static member
  • Java applet as stand-alone Windows application?
  • Why winpcap requires both .lib and .dll to run?
  • How to format a variable of double type
  • Revoking OAuth Access Token Results in 404 Not Found
  • Error creating VM instance in Google Compute Engine
  • How to set the response of a form post action to a iframe source?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android