小编use*_*567的帖子

C# - 检查枚举元素上是否存在属性

我有以下情况:

enum Header
{
    Sync,
    [OldProtocol] Keepalive,
    Ping,
    [OldProtocol] Auth,
    [OldProtocol] LoginData
    //...
}
Run Code Online (Sandbox Code Playgroud)

我需要获取一个OldProtocolAttribute定义了元素的数组.我注意到该Attribute.IsDefined()方法及其重载显然不支持这种情况.

我的问题是:

  • 有没有办法在不使用解决方案的任何部分的情况下解决问题typeof(Header).GetField()
  • 如果没有,解决它的最佳方法是什么?

c# enums attributes elements defined

11
推荐指数
2
解决办法
3165
查看次数

取消订阅通过ref关键字传递给委托方法的委托?

我有以下课程:

public class Terminal : IDisposable
{
    readonly List<IListener> _listeners;

    public Terminal(IEnumerable<IListener> listeners)
    {
        _listeners = new List<IListener>(listeners);
    }

    public void Subscribe(ref Action<string> source)
    {
        source += Broadcast;
        //Store the reference somehow?
    }

    void Broadcast(string message)
    {
        foreach (var listener in _listeners) listener.Listen(message);
    }

    public void Dispose()
    {
        //Unsubscribe from all the stored sources?
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经搜索了一段时间,似乎无法存储使用ref关键字传递的参数.尝试将源参数添加到列表或将其分配给字段变量不允许它保持对实际委托的原始引用的引用; 所以我的问题是:

  • 有没有办法取消订阅所有来源而不再传递他们的参考?
  • 如果没有,为了支持它,如何更改类,但仍然通过一个方法传递委托来维护订阅?
  • 是否有可能在不使用Reflection的情况下实现它?
  • 是否有可能在没有将委托/事件包装在类中然后将类作为订阅参数传递的情况下实现它?

谢谢.

编辑:似乎没有使用包装器或反射,没有解决给定的问题.我的目的是使类尽可能地可移植,而不必将代理包装在辅助类中.谢谢大家的贡献.

c# parameters delegates ref unsubscribe

9
推荐指数
1
解决办法
470
查看次数

端口RSA加密Java代码到C#

我正在尝试将以下Java代码移植到C#等价物:

public static String encrypt(String value, String key) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    byte[] bytes = value.getBytes(Charset.forName("UTF-8"));
    X509EncodedKeySpec x509 = new X509EncodedKeySpec(DatatypeConverter.parseBase64Binary(key));
    KeyFactory factory = KeyFactory.getInstance("RSA");
    PublicKey publicKey = factory.generatePublic(x509);
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    bytes = cipher.doFinal(bytes);
    return DatatypeConverter.printBase64Binary(bytes);
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我设法使用.NET的BouncyCastle库在C#中编写以下内容:

public static string Encrypt(string value, string key)
    {
        var bytes = Encoding.UTF8.GetBytes(value);
        var publicKeyBytes = Convert.FromBase64String(key);
        var asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes);
        var rsaKeyParameters = (RsaKeyParameters) asymmetricKeyParameter;
        var cipher = CipherUtilities.GetCipher("RSA");
        cipher.Init(true, rsaKeyParameters);
        var processBlock = …
Run Code Online (Sandbox Code Playgroud)

c# java porting rsa bouncycastle

6
推荐指数
1
解决办法
2275
查看次数

几乎相同的方法之间的极好的性能差异

在处理项目时,我意外地注意到只有一个额外(未使用)参数的相同方法运行甚至比另一个运行快十倍,并且启用了优化.

type Stream () =
    static member private write (x, o, a : byte[]) = (for i = 0 to 3 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256)); 4
    static member private format f x l = Array.zeroCreate l |> fun a -> (f(x, 0, a) |> ignore; a)
    static member private format1 f x l o = Array.zeroCreate l |> fun a -> (f(x, 0, a) |> ignore; a)
    static member Format …
Run Code Online (Sandbox Code Playgroud)

compiler-construction optimization performance f#

2
推荐指数
1
解决办法
216
查看次数