程序中呼叫Class(类)

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

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

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

ExpandedBlockStart.gif 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:

ExpandedBlockStart.gif 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:

ExpandedBlockStart.gif 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:

ExpandedBlockStart.gif 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:

ExpandedBlockStart.gif 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:

ExpandedBlockStart.gif 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:

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

 

三种方法执行的结果:

 

 

转载于:https://www.cnblogs.com/insus/archive/2011/11/09/2242410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值