强制演员

编程入门 行业动态 更新时间:2024-10-10 06:18:15
本文介绍了强制演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从类外部访问页面对象的视图状态 (在帮助器类中),所以我创建了一个继承自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

更多推荐

强制演员

本文发布于:2023-11-25 11:56:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:演员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!