Reputation: 299
I have a c# application where I have methods say, method1 , method2 .... method n. Each of these methods are features. I dont want my application to execute all the features all the time but, it should be configurable. So I am in need of a config file where what features to execute. Please suggest me a method to do this. I am thinking of using an XML file to store the method names and using reflection to invoke them. But, is there any PROFESSIONAL way to do this ?
Upvotes: 0
Views: 207
Reputation: 54433
Your question consists of several subqusetions. Working professionally imo begins with breaking down a problem into its most elmental parts.
Let's see:
You didn't tell us; could be the same application or a diferent one or a human being. In the latter case make sure the data are not misspelled. The usual way is to list all valid names and have the user simply add or remove a comment mark at the start of a line.
if else if
block or for something more fancy create a Dictionary<string, Action>
or the like..Upvotes: 0
Reputation: 3073
C# applications have an app.config (for desktop apps) or web.config (for asp.net apps) file. In this file you can specify settings, like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="something" value="1"/>
</appSettings>
</configuration>
In your application code, you can access these settings like this:
var something = ConfigurationManager.AppSettings["something"];
Note: You'll have to add a reference to
System.Configuration
to access theConfigurationManager
.
When you compile your application, the app.config gets transformed into YourApp.exe.config, and you can deploy it alongside your application to control the settings without having to recompile your application.
You can read more about the configuration manager, and the app.settings on MSDN.
Upvotes: 1