I've placed a static class TestClass in the App_Code folder. The class contains a static method TestMethod. From Default.aspx.cs via Button_Click method, I'm trying to invoke TestMethod. - test = TestClass.TestMethod(). This gives error: 'TestClass' is inaccessible due to its protection level.
It feels like the static class and the _Default class should be placed into a common namespace, but this would "exclude" the Default.aspx controls references.
What am I doing wrong?
You need to declare TestClass
as public:
public static class TestClass
{
public static SomeType TestMethod()
{
}
}
The default visibility is internal
for types and private
for members of a type definition. The App_Code
folder gets compiled into its own assembly, different from the assembly that is created when compiling the code behinds. internal
types cannot be shared between assemblies (that's not 100% true but true in this case), hence why you are having this problem.
Try to make it public static.
public class TestClass{
public static TestMethod(){}
}
Make sure TestClass is public:
public class TestClass
{
}
The default when adding a class to Visual Studio is to omit the "public".
确保方法原型上至少具有“内部”作用域。
Just because your class is static doesn't mean that it is accessible to all code/assemblies. Make sure that your static class has an access modifier (ie public) that is accessible by your web form.
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.