通过方法作为具有不同签名的参数 [英] pass method as parameter with different signature

查看:45
本文介绍了通过方法作为具有不同签名的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我要寻找的是一生中第一次使用委托,而问题是我没有看到教程或方法在整个网络中都具有不同的输入签名的问题,最后直到现在:|

我的问题是我想用2种方法编写一个类,这些方法使用其他方法,它们具有不同的输入签名和相同的输出...

像:

我有这种方法:

Hello guys,

the thing i''m looking for is to use delegate for first time in whole my life, and the problem is i saw no tutorial or problem that the method have diffrent input signature in whole network, at last till now :|

my problem is that i wanna write a class, with 2 method, that these method use an other methods, with with differente input signature and same output...

like :

i have this method :

myMethod(method)

{
   byte[] b=method;
}




然后用户通过传递其希望的方法来调用这些方法...

用户1:




and then users call these method with passing their wished method...

user 1:

byte[] data;
string skey;

myMethod(AEncMethod(data,skey));




和用户2:




and user 2:

string otherData;
string otherKey;
CipherMode cipherMode;

myMethod(BEncMethod(otherData,otherKey,cipherMode));



因为这是我第一次使用委托,所以如果您也可以解释它并使用简单的编码,则最好是帮助我理解和实现自己的委托.

我也正在使用.Net 2,因为结果应用程序将使所有新老用户都使用diff os,所以我想使用旧框架,所以不希望使用Func或Action,尽管事件没有使用Func:D

我也想知道是否也应该取消泛型实体类的定义以传递我的参数.



since it''s first time i''m using delegate, if you also can explain it and use simple coding it will be best to help me understand and implement my own.

also i''m using .Net 2 since the result application gonna have all old and new user with diff os, i want use old framework so no Func or Action, though event didnt see one with Func :D

i''m wonder also if i should defain generics entity classes to pass my parameters also.

推荐答案

不是您问题的具体答案,但您可能想研究一下策略模式 [ ^ ]
Not a specific answer to your question but you may want to research the Strategy Pattern[^]


Hello Hassan,

使用接口而不是委托(请参见下文).

顺便说一句:与界面相比,代表通常是第二选择.

干杯

安迪




简约的加密接口:

Hello Hassan,

use interfaces instead of delegages (see below).

BTW: Delegates are often second choice, compared with interfaces.

Cheers

Andi




Minimalistic crypto interface:

/// <summary>basic crypto interface</summary>
public interface ICryptoAlgorithm
{
    /// <summary>encrypt</summary>
    /// <param name="plainData">plain data bytes</param>
    /// <returns>encrypted data bytes</returns>
    byte[] Encrypt(byte[] plainData);
    /// <summary>decrypt</summary>
    /// <param name="encryptedData">encrypted data bytes</param>
    /// <returns>plain data bytes</returns>
    byte[] Decrypt(byte[] encryptedData);
}



您自己对特定加密算法的封装(实现ICryptoAlgorithm):



Your own encapsulation of the specific crypto algorithm (implements ICryptoAlgorithm):

public class MyCrypto: ICryptoAlgorithm
{
    TypeA _a;
    TypeB _b;
    public MyCrypto(TypeA a, TypeB b)
    {
       _a = a;
       _b = b;
       ...
    }

    public byte[] Encrypt(byte[] plainData)
    {
        // use a, b, etc.
        ...
    }

    public byte[] Decrypt(byte[] encryptedData)
    {
        // use a, b, etc.
        ...
    }
}



您自己的函数需要一个具体的ICryptoAlgorithm(而不是委托):



Your own function takes a concrete ICryptoAlgorithm (instead of a delegate):

void Save(ICryptoAlgorithm alg, byte[] plainData)
{
    try
    {
        var encoded = alg.Encrypt(plainData);
        ...
    }
    catch (Exception exc)
    {
        Console.WriteLine(exc.ToString());
    }
}



用法:



Usage:

ICryptoAlgorithm alg = new MyCrypto(x, y);
byte[] plainData = ...
Save(alg, plainData);


检查此论坛
http://stackoverflow.com/questions/2082615/pass-method-as- parameter-using-c-sharp [ ^ ]
--NDK
check this forum
http://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp[^]
--NDK


这篇关于通过方法作为具有不同签名的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆