• 程序中呼叫Class(类)


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

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

    ICallable
    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(类),它就是主角,是准备程序可以呼叫的类别。类别实作了上面的接口。

    Author
    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:

    MethodA.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:

    MethodA.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:

    MethidB.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:

    MethodB.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:

    MethodC.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:

    MethodC.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();
        }
    }

    三种方法执行的结果:

  • 相关阅读:
    openmediavault 5.5.23 安装插件失败的解决方案
    qt下载地址
    qt 5.12 增加 mysql驱动
    选基金标准
    关注几个基金
    调仓的几个问题
    要读的书
    ubuntu 20.04 LTS 安装webmin
    set的常见用法
    斜率优化dp([HNOI2008]玩具装箱)
  • 原文地址:https://www.cnblogs.com/insus/p/2242410.html
Copyright © 2020-2023  润新知