我想从类外部访问页面对象的视图状态 (在帮助器类中),所以我创建了一个继承自Page的类,并公开了一个 返回viewstate对象的属性。我的帮助类我将 页面转换为我的页面类并访问公开的属性,但是强制转换导致 指定的强制转换无效异常。我曾经在delphi和 c ++中一直访问受保护的东西,是否可以在.NET中使用?
解决方案" Jeremy Chapman" < me@here>在消息中写道 新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ... 我想要从类的外部访问页面对象的视图状态(在辅助类中),所以我创建了一个继承自Page的类,并公开了一个返回viewstate对象的属性。我的帮助类我将页面转换为我的页面类并访问公开的属性,但是强制转换导致指定的强制转换无效异常。我曾经在delphi 和c ++中一直这样做以访问受保护的东西,是否可以在.NET中?
在.NET中可以进行强制转换。发布实际的源代码,它会更容易 来帮助你。 - Oliver
我认为你认为你曾经在Delphi中这么做过,但它根本不是 。为什么?因为你只能将一个类型转换为它所属的类型,它是一个类型,它是它的类型,还是它继承的类型。由于您的自定义类 继承了System.Web.UI.Page,您可以将IT转换为System.Web.UI.Page类, 但是您无法投射System.Web.UI.Page类作为您的自定义类。 然而,请加油助威。事实上,你的解决方案是不必要的复杂。 请记住,你的页面不是System.Web.UI.Page类,而是 " YourClassName" class,继承System.Web.UI.Page。 IT可以使用自己的ViewState访问 ,并可以通过您选择的任何方法公开它。 - HTH, Kevin Spencer Microsoft MVP ..Net开发人员 期待未被接受。 " Jeremy Chapman" < me@here>在消息中写道 新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...
我想要从类的外部访问页面对象的视图状态(在辅助类中),所以我创建了一个继承自Page的类,并公开了一个返回viewstate对象的属性。我的帮助类我将页面转换为我的页面类并访问公开的属性,但是强制转换导致指定的强制转换无效异常。我曾经在delphi 和c ++中一直这样做以访问受保护的东西,是否可以在.NET?
这里有一些样本来源。 使用系统; 名称空间Mynamespace { / //< summary> ///页面的帮助函数 ///< / summary> 公共类HelperClass { public HelperClass { // // TODO:Add构造函数逻辑这里 // } 私有类CustomPage:System.Web.UI.Page { public CustomPage() { } public System.Web.UI.StateBag PublishViewState { get { 返回ViewState; } } } public static void HelperTest(System.Web.UI.Page pPage) { CustomPage pPageCustom =(CustomPage)pPage; pPageCustom.PublishViewState [" test"] = DateTime.Now; } } } " Oliver Wong" <流*** @ castortech>在消息中写道 新闻:Q18Le.195006
I want to access the viewstate of a page object from outside of the class (in a helper class), so I made a class inherited from Page, and exposed a property that returns the viewstate object. I my helper class I cast the page to my page class and access the exposed property, but the cast causes an ''Specified cast is not valid'' exception. I used to do this in delphi and c++ all the time to access protected stuff, is it possible in .NET?
解决方案 "Jeremy Chapman" <me@here> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...I want to access the viewstate of a page object from outside of the class(in a helper class), so I made a class inherited from Page, and exposed aproperty that returns the viewstate object. I my helper class I cast thepage to my page class and access the exposed property, but the cast causesan ''Specified cast is not valid'' exception. I used to do this in delphiand c++ all the time to access protected stuff, is it possible in .NET?Casting is possible in .NET. Post actual source code and it''ll be easier to help you. - Oliver
I think you THINK you used to so this in Delphi, but it is simply not possible. Why? Because you can only cast a type as a type that it IS, that is, the type that it is, or a type that it inherits. Since your custom class inherits System.Web.UI.Page, you can cast IT as a System.Web.UI.Page class, but you can not cast a System.Web.UI.Page class as your custom class. However, be of good cheer. In fact, your solution is needlessly complex. Remember that your Page is not a System.Web.UI.Page class, but a "YourClassName" class, which inherits System.Web.UI.Page. IT has access to its own ViewState, and can expose it via any method you choose. -- HTH, Kevin Spencer Microsoft MVP ..Net Developer Expect the unaccepted. "Jeremy Chapman" <me@here> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...I want to access the viewstate of a page object from outside of the class(in a helper class), so I made a class inherited from Page, and exposed aproperty that returns the viewstate object. I my helper class I cast thepage to my page class and access the exposed property, but the cast causesan ''Specified cast is not valid'' exception. I used to do this in delphiand c++ all the time to access protected stuff, is it possible in .NET?Here is some sample source. using System; namespace Mynamespace { /// <summary> /// Helper functions for a page /// </summary> public class HelperClass { public HelperClass { // // TODO: Add constructor logic here // } private class CustomPage : System.Web.UI.Page { public CustomPage() { } public System.Web.UI.StateBag PublishViewState { get { return ViewState; } } } public static void HelperTest(System.Web.UI.Page pPage) { CustomPage pPageCustom = (CustomPage)pPage; pPageCustom.PublishViewState["test"] = DateTime.Now; } } } "Oliver Wong" <ow***@castortech> wrote in message news:Q18Le.195006更多推荐
强制演员
发布评论