Access method in class library
I've moved a class in my project into a class Library in the same solution. I've added a reference in the web project to the class library.
How do I access the met开发者_运维问答hods in the class library?
Make sure the method and class are public and instantiate to use it:
YourClass yourClass = new YourClass();
yourClass.YourMethod();
Or, if your method is static:
YourClass.YourMethod();
Make sure to include the namespace of your class is your target classes using statements, i.e.
using ClassLibrary;
If you're unsure of the namespace of YourClass
you can find it within the Class file at the top, directly beneath your using statements. Or you can mouse over the YourClass
text in the Visual Studios Editor and then select the namespace by clicking the button that appears.
- Add a reference to the class library you moved it to.
If the method is public (and not static) you'll need to instantiate the class :
YourClass yourClass = new YourClass(); yourClass.YourMethod();
If the method is public and static then you can call it directly :
YourClass.YourMethod();
and make sure not to forget the using statement, preffered to place at the top of your file:
using MyClassNamespace;
精彩评论