用AspJpeg组件,按宽高比例,真正生成缩略图

laifangsong's .NET blog

取长补短,精益求精。 (打个广告,想做手机网站和asp/asp.net网站的可以跟我联系.QQ:25313644)
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

委托:手工引发委托链中异常的例子

Posted on 2006-04-14 16:24  laifangsong  阅读(338)  评论(0编辑  收藏  举报
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace AspNetTest.Common
{
    
/// <summary>
    
/// Define_Delegate_handwork 的摘要说明。
    
/// </summary>

    public class Define_Delegate_handwork : System.Web.UI.Page
    
{
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            MathOp mathOp 
= new MathOp(1,0);
    
            mathOp.opDelegate 
= null;
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Add);
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Subtract);
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Divide);    
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Multiply);

            
object[] args = new object[2];
            args[
0= mathOp.Op1;
            args[
1= mathOp.Op2;
            Delegate[] operaters 
= mathOp.opDelegate.GetInvocationList();
            
foreach(Delegate operater in operaters)
            
{
                
try
                
{
                    operater.DynamicInvoke(args);
                }

                
catch(Exception ex)
                
{
                    Response.Write(
"<font color=red>被零除,引发一个异常,但并不影响委托链的其他实例运行!</font><br>" + "异常信息:<font color=gray>" + ex.Message + "</font><br><br>");
                }

            }

        }

        
        
public class MathOp
        
{
            
public MathOp(int op1, int op2)
            
{
                
this.op1 = op1;
                
this.op2 = op2;
            }

            
public delegate void OpDelegate(int op1, int op2);
            
public OpDelegate opDelegate;
            
private int op1;
            
private int op2;
            
public int Op1
            
{
                
get
                
{
                    
return this.op1;
                }

            }

            
public int Op2
            
{
                
get
                
{
                    
return this.op2;
                }

            }

        }

        
        
public class OperateFunc
        
{
            
public static void Add(int op1, int op2)
            
{
                
int result = (int)op1+op2;
                HttpContext.Current.Response.Write(
"加:" + result + "<br>");
            }

            
public static void Subtract(int op1, int op2)
            
{
                
int result = (int)op1-op2;
                HttpContext.Current.Response.Write(
"减:" + result + "<br>");
            }

            
public static void Multiply(int op1, int op2)
            
{
                
int result = (int)op1*op2;
                HttpContext.Current.Response.Write(
"乘:" + result + "<br>");
            }

            
//表示整除
            public static void Divide(int op1, int op2)
            
{
                
int result = (int)op1/op2;
                
//注释该行,故意引发异常
                
//return op2==0 ? 0 : (int)op1/op2;
                HttpContext.Current.Response.Write("除:" + result + "<br>");    
            }

        }

        
        
Web 窗体设计器生成的代码
    }

}