这个Extension方法线程安全吗?
public static class Extensions
{
public static void Raise<T>(this EventHandler<T> handler,
object sender, T args) where T : EventArgs
{
if (handler != null) handler(sender, args);
}
}
Run Code Online (Sandbox Code Playgroud)
或者我需要将其改为此吗?
public static class Extensions
{
public static void Raise<T>(this EventHandler<T> handler,
object sender, T args) where T : EventArgs
{
var h = handler;
if (h!= null) h(sender, args);
}
}
Run Code Online (Sandbox Code Playgroud) 设计问题 - 多态事件处理
我目前正在尝试减少当前项目中的事件句柄数量.我们有多个通过USB发送数据的系统.我目前有一个例程来读取消息并解析初始标头详细信息以确定消息来自哪个系统.标题稍有不同,所以我创建的EventArgs不一样.然后我通知所有"观察员"这一变化.所以我现在所拥有的是以下内容:
public enum Sub1Enums : byte
{
ID1 = 0x01,
ID2 = 0x02
}
public enum Sub2Enums : ushort
{
ID1 = 0xFFFE,
ID2 = 0xFFFF
}
public class MyEvent1Args
{
public Sub1Enums MessageID;
public byte[] Data;
public MyEvent1Args(Sub1Enums sub1Enum, byte[] data)
{
MessageID = sub1Enum;
Data = data;
}
}
public class MyEvent2Args
{
public Sub2Enums MessageID;
public byte[] Data;
public MyEvent2Args(Sub2Enums sub2Enum, byte[] data)
{
MessageID = sub2Enum;
Data = data;
}
}
Run Code Online (Sandbox Code Playgroud)
Form1代码
public …Run Code Online (Sandbox Code Playgroud) 我想看看一个对象是否是C#中的内置数据类型
如果可能的话,我不想检查所有这些.
也就是说,我不希望这样做:
Object foo = 3;
Type type_of_foo = foo.GetType();
if (type_of_foo == typeof(string))
{
...
}
else if (type_of_foo == typeof(int))
{
...
}
...
Run Code Online (Sandbox Code Playgroud)
更新
我试图递归创建一个PropertyDescriptorCollection,其中PropertyDescriptor类型可能不是内置值.所以我想做这样的事情(注意:这还不行,但我正在努力):
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection cols = base.GetProperties(attributes);
List<PropertyDescriptor> list_of_properties_desc = CreatePDList(cols);
return new PropertyDescriptorCollection(list_of_properties_desc.ToArray());
}
private List<PropertyDescriptor> CreatePDList(PropertyDescriptorCollection dpCollection)
{
List<PropertyDescriptor> list_of_properties_desc = new List<PropertyDescriptor>();
foreach (PropertyDescriptor pd in dpCollection)
{
if (IsBulitin(pd.PropertyType))
{
list_of_properties_desc.Add(pd);
}
else
{
list_of_properties_desc.AddRange(CreatePDList(pd.GetChildProperties()));
}
}
return list_of_properties_desc;
}
// …Run Code Online (Sandbox Code Playgroud) 加密是在java中:
String salt = "DC14DBE5F917C7D03C02CD5ADB88FA41";
String password = "25623F17-0027-3B82-BB4B-B7DD60DCDC9B";
char[] passwordChars = new char[password.length()];
password.getChars(0,password.length(), passwordChars, 0);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passwordChars, salt.getBytes(), 2, 256);
SecretKey sKey = factory.generateSecret(spec);
byte[] raw = _sKey.getEncoded();
String toEncrypt = "The text to be encrypted.";
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, skey);
AlgorithmParameters params = cipher.getParameters();
byte[] initVector = params.getParameterSpec(IvParameterSpec.class).getIV();
byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());
Run Code Online (Sandbox Code Playgroud)
虽然解密是在c#中:
string hashAlgorithm = "SHA1";
int passwordIterations = 2;
int keySize = 256;
byte[] saltValueBytes = …Run Code Online (Sandbox Code Playgroud) 我已经为我的应用程序设置了一个部署项目.问题是我想在安装期间显示应用程序版本(例如MyApplication 1.2.3.1),以便用户在安装之前可以看到该版本.
我能想到的唯一方法是在Welcome对话框中修改WelcomeText.是否有更简单或更优雅的方式来实现这一目标?
我想这样实现:
namespace PIMP.Web.TestForum.Models
{
public class ThreadModel : PagedList<T>
{
Run Code Online (Sandbox Code Playgroud)
但我得到ErrorMessage:
找不到类型或命名空间名称"T"(您是否缺少using指令或程序集引用?)
我该怎么做才能避免它?
[Flags]
public enum MyEnum
{
None = 0,
Setting1 = (1 << 1),
Setting2 = (1 << 2),
Setting3 = (1 << 3),
Setting4 = (1 << 4),
}
Run Code Online (Sandbox Code Playgroud)
我需要能够以某种方式循环每个可能的设置并将设置组合传递给函数.可悲的是,我一直无法弄清楚如何做到这一点
我已经"下载了丢失的软件包"

项目文件最初是在VS 2013中创建的
build.proj使用MSBuild任务来构建解决方案.
<MSBuild Projects ="$(root)\src\MySolution.sln" ContinueOnError ="false" Properties="Configuration=$(Configuration)">
<Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>
</MSBuild>
Run Code Online (Sandbox Code Playgroud)
我的google-fu失败了我,任何和所有想法都会得到很好的收到.