为了账号安全,请及时绑定邮箱和手机立即绑定

程序中呼叫Class(类)

标签:
JavaScript

如何在程序中呼叫你写好的Class(类)。为了实现这些功能,下面做了一个小小的例子。第一步,我们得把呼叫的类写好。

首先写一个interface(接口),这个接口有一个方法Call():

5acf07340001405b00110016.jpgICallable using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ICallable
/// </summary>
namespace Insus.NET
{
    public interface ICallable
    {
        void Call();
    }
}

 

下面写一个Class(类),它就是主角,是准备程序可以呼叫的类别。类别实作了上面的接口。

5acf07340001405b00110016.jpgAuthor using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Author
/// </summary>
namespace Insus.NET
{
    public class Author : ICallable
    {
        string _name;

        public Author(string name)
        {
            this._name = name;
        }

        public void Call()
        {
            HttpContext.Current.Response.Write("Hello," + _name);
        }
    }
}

 

下面分别用三种方式来呼叫类别,第一种,最普通的方法:Author obj = new Author(); 。

MethodA.aspx:

5acf07340001405b00110016.jpgMethodA.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MethodA.aspx.cs" Inherits="MethodA" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

 

MethodA.aspx.cs:

5acf07340001405b00110016.jpgMethodA.aspx.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public partial class MethodA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Author objAuthor = new Author("Insus.NET.");
        objAuthor.Call();
    }
}

 

第二种方法:

MethodB.aspx:

5acf07340001405b00110016.jpgMethidB.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MethodB.aspx.cs" Inherits="MethodB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

 

MethodB.aspx.cs:

5acf07340001405b00110016.jpgMethodB.aspx.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public partial class MethodB : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Type oType = typeof(Author);
        Author objAuthor = (Author)Activator.CreateInstance(oType, new object[] { "Insus.NET." });
        objAuthor.Call();
    }
}

 

第三种方法,使用的Reflection(反射):

MethodC.aspx:

5acf07340001405b00110016.jpgMethodC.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MethodC.aspx.cs" Inherits="MethodC" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

 

MethodC.aspx.cs:

5acf07340001405b00110016.jpgMethodC.aspx.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Insus.NET;

public partial class MethodC : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string classFullName = "Insus.NET.Author";
        Assembly assembly = Assembly.Load("App_Code");
        Type objType = assembly.GetType(classFullName);
        ICallable obj = (ICallable)Activator.CreateInstance(objType, new object[] { "Insus.NET." });
        obj.Call();
    }
}

 

三种方法执行的结果:


 

 

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消