简体   繁体   English

如何创建变量名称为'toString'类型的动态类?

[英]How do I create a dynamic class with variable name of type 'toString'?

Thank you for reading my article. 感谢您阅读我的文章。

I am using a UserControl of asp.net in company webpage. 我在公司网页中使用asp.net的UserControl UserControl can not use inheritance class, because that already inherits System.Web.UI.UserControl class. UserControl不能使用继承类,因为该类已经继承了System.Web.UI.UserControl类。

So I want to create a dynamic class with the name of the parent page. 所以我想用父页面的名称创建一个动态类。

Below is my code. 下面是我的代码。

string userNo = string.Empty;
string virtualPath = Page.AppRelativeVirtualPath.ToString();
virtualPath = virtualPath.Substring(virtualPath.LastIndexOf("/") + 1);

//I want to use the generated class by referring to the variable name of "virtualPath" in the "here" text below.
//userNo = Uact.Framework.Common.Decryptor.Decrypt((("here!")Context.Handler).UserNo);

switch (virtualPath.ToUpper().Trim()){
    case "PROJECTSIGNWRITE.ASPX" :
        userNo = Uact.Framework.Common.Decryptor.
                    Decrypt(((ProjectSignWrite)Context.Handler).UserNo);
        break;

    case "PROJECTREPORTVIEW.ASPX" :
        userNo = Uact.Framework.Common.Decryptor.
                    Decrypt(((ProjectReportView)Context.Handler).UserNo);
        break;

    case "PROJECTCOMPLETE.ASPX" :
        userNo = Uact.Framework.Common.Decryptor.
                    Decrypt(((ProjectComplete)Context.Handler).UserNo);
        break;
}

Instead of using inheritance, use an interface. 不要使用继承,而要使用接口。

Declare an interface with the property UserNo 声明具有属性UserNo的接口

public interface IHasUserNo
{
     string UserNo {get; set;}
}

Then have your classes like for example ProjectSignWrite implement this interface. 然后让您的类(例如ProjectSignWrite实现此接口。

public class ProjectSignWrite : UserControl, IHasUserNo
{
    .....
}

You can then simply write: 然后,您可以简单地编写:

userNo = Uact.Framework.Common.Decryptor.Decrypt(((IHasUserNo)Context.Handler).UserNo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM