I would like to declare an alias to the static class : System.Configuration.ConfigurationManager;
I would like this alias to be available to all the methods of my class so I've tried to do that :
public class MyClass
{
using conf = System.Configuration.ConfigurationManager;
public void MethodOne()
{
string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
}
}
But the above raises the following error : Invalid token 'using' in class, struct, or interface member declaration
Is there something I can do to alias this configuration manager class ?
You can't have a using
statement inside a class. The error message you are getting is very specific in this regards.
To solve it, simply put it outside of your class:
using conf = System.Configuration.ConfigurationManager;
public class MyClass
{
public void MethodOne()
{
string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
}
}
using
to create aliases must be used outside a class scope, but in in the assembly or namespace scope!
// Here here here here here here :)
using conf = System.Configuration.ConfigurationManager;
public class MyClass
{
public void MethodOne()
{
string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
}
}
using
directives can be used to create an alias within the scope of the current .cs
file.
using conf = System.Configuration.ConfigurationManager;
namespace MyNamespace
{
public class MyClass
{
public void MethodOne()
{
string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
}
}
}
See this documentation for more information
You must place that using statement outside of your class definition just like the compiler is telling you.
using conf = System.Configuration.ConfigurationManager;
public class MyClass
{
public void MethodOne()
{
string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
}
}
Please refer to the C# language specification under section 9.4 "Using directives".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.