我有以下情况:
enum Header
{
Sync,
[OldProtocol] Keepalive,
Ping,
[OldProtocol] Auth,
[OldProtocol] LoginData
//...
}
Run Code Online (Sandbox Code Playgroud)
我需要获取一个OldProtocolAttribute定义了元素的数组.我注意到该Attribute.IsDefined()方法及其重载显然不支持这种情况.
我的问题是:
typeof(Header).GetField()?我有以下课程:
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关键字传递的参数.尝试将源参数添加到列表或将其分配给字段变量不允许它保持对实际委托的原始引用的引用; 所以我的问题是:
谢谢.
编辑:似乎没有使用包装器或反射,没有解决给定的问题.我的目的是使类尽可能地可移植,而不必将代理包装在辅助类中.谢谢大家的贡献.
我正在尝试将以下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) 在处理项目时,我意外地注意到只有一个额外(未使用)参数的相同方法运行甚至比另一个运行快十倍,并且启用了优化.
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) c# ×3
attributes ×1
bouncycastle ×1
defined ×1
delegates ×1
elements ×1
enums ×1
f# ×1
java ×1
optimization ×1
parameters ×1
performance ×1
porting ×1
ref ×1
rsa ×1
unsubscribe ×1