问题描述
HI,
我正在.aspx.cs文件中创建一个具有属性和方法的类.我在第一次发布页面时调用此方法,并设置属性值.现在,我想在App_Code中存在的文件中使用这些属性.可以做到这一点.如果是这样,该怎么办.
等待回复.
谢谢
I am creating a class in .aspx.cs file, which has properties and a method. I call this method when the page is posted for the first time and set the propery values. Now i want to use these properties in a file which is present in App_Code. Can this be done. if so how can it be done.
Waiting for the reply.
Thanks
推荐答案
class MyClass {
internal static void ClassMethod() { //cannot address members of any instance, only other static members
} //ClassMethod
internal void InstanceMethod() { /* ... */ this.Field = /* ... */ }
int Field;
}
MyClass ClassMethod(); //no instance needed, no "this" reference passed
MyClass instance = new MyClass( /* ... */ );
instance.InstanceMethod(); // during execution of the body of the method this == instance
剩下的方面是访问修饰符.默认情况下,在private
中访问.访问修饰符internal
(请参见上面的示例)打开来自任何其他类的访问,并将任何代码放置在同一程序集中.该程序集可以被其他程序集引用,但是如果internal
被public
替换,它们可以访问上面显示的方法.
请注意,访问修饰符不提供任何保护(但是对于避免意外错误和隔离代码的部分非常有帮助,因此最好不要过度发布"任何内容以提供尽可能少的访问权限).使用反射,可以访问每个成员,而无论访问修饰符是什么.
也可以使用Reflection进行呼叫,但这是一个完全不同的故事.
Remaining aspect is access modifiers. By default, access in private
. The access modifier internal
(see the sample above) opens up access from any other class, any code placed in the same assembly. This assembly can be references by other assemblies, but they can get access to the methods shown above is internal
is replaced with public
.
Note, that access modifiers does not provide any protection (but very helpful to avoid accidental mistakes and isolate parts of code, so it''s the best not to "over-publish" anything providing minimal possible access). With Reflection, every member can be accessed regardless access modifier.
A call also can be done with Reflection, but this is quite a different story.
这篇关于将一个类别的对象调用到另一个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!