var mergedInstance = MergeFactory<InterfaceOne, InterfaceTwo>();
((InterfaceOne)mergedInstance).InterfaceOneMethod();
((InterfaceTwo)mergedInstance).InterfaceTwoMethod();
Run Code Online (Sandbox Code Playgroud)
任何人都可以推荐一个设计模式或确切的语法,使这样的工作?
在MergeFactory中,我正在想象这样的事情:
MergeFactory<Iface1, Iface2>() :
where Iface1: IMergeable, Iface2: IMergeable
{
IMergeable instance = Iface1Factory.CreateIface1Instance();
instance.Merge(Iface2Factory.CreateIface2Instance());
}
Run Code Online (Sandbox Code Playgroud) 我使用以下C#源代码使用AES(ECB 256)加密纯文本:
public static string Encode(string PlainText)
{
byte[] Key = ASCIIEncoding.UTF8.GetBytes("12345678901234567890123456789012");
string encrypted = null;
RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;
rj.KeySize = 256;
rj.Key = Key;
rj.GenerateIV();
byte[] IV = rj.IV;
rj.Mode = CipherMode.ECB;
rj.Padding = PaddingMode.Zeros;
try
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(PlainText);
sw.Close();
sw.Dispose();
}
cs.Close();
cs.Dispose();
}
byte[] encryptArray = ms.ToArray();
encrypted = …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个C#应用程序.
我需要使用带有组合框的枚举来获取所选月份.我有以下内容来创建枚举:
enum Months
{
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
Run Code Online (Sandbox Code Playgroud)
然后我使用以下内容初始化组合框:
cboMonthFrom.Items.AddRange(Enum.GetNames(typeof(Months)));
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,但问题是当我尝试获取所选月份的选定枚举值
要从组合框中获取枚举器的值,我使用了以下内容:
private void cboMonthFrom_SelectedIndexChanged(object sender, EventArgs)
{
Months selectedMonth = (Months)cboMonthFrom.SelectedItem;
Console.WriteLine("Selected Month: " + (int)selectedMonth);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行上面的代码时,会出现一个错误,指出System.InvalidCastException发生了类型的第一次机会异常.
我做错了什么.
感谢您的任何帮助,您可以提供
我想做以下事情:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SomeStruct
{
public byte SomeByte;
public int SomeInt;
public short SomeShort;
public byte SomeByte2;
}
Run Code Online (Sandbox Code Playgroud)
是否有替代方案,因为紧凑框架不支持Pack?
更新:显式设置结构并为每个提供FieldOffset都不起作用,因为它不会影响结构的打包方式
Update2:如果您尝试以下操作,CF程序甚至不会运行,因为结构的打包方式如下:
[StructLayout(LayoutKind.Explicit, Size=8)]
public struct SomeStruct
{
[FieldOffset(0)]
public byte SomeByte;
[FieldOffset(1)]
public int SomeInt;
[FieldOffset(5)]
public short SomeShort;
[FieldOffset(7)]
public byte SomeByte2;
}
Run Code Online (Sandbox Code Playgroud)
我知道这似乎很难相信,但如果你尝试它,你会看到.将它添加到CF项目并尝试运行它,您将获得TypeLoadException.将偏移分别更改为0,4,8,10并且它将起作用(但是大小最终为12).
我希望也许某人有一个使用反射的解决方案可能单独编组每个字段类型的大小(涉及递归以处理结构或类型数组中的结构).
有没有办法在C#中重载事件+ =和 - =运算符?我想做的是拿一个事件监听器并将其注册到不同的事件.所以像这样:
SomeEvent += new Event(EventMethod);
Run Code Online (Sandbox Code Playgroud)
然后它不是附加到SomeEvent,而是实际上附加到不同的事件:
DifferentEvent += (the listener above);
AnotherDiffEvent += (the listener above);
Run Code Online (Sandbox Code Playgroud)
谢谢
如何正确同步?目前有可能SetData在e.WaitOne()完成后调用,因此d可能已经设置为另一个值.我试图插入锁,但它导致死锁.
AutoResetEvent e = new AutoResetEvent(false);
public SetData(MyData d)
{
this.d=d;
e.Set(); // notify that new data is available
}
// This runs in separate thread and waits for d to be set to a new value
void Runner()
{
while (true)
{
e.WaitOne(); // waits for new data to process
DoLongOperationWith_d(d);
}
}
Run Code Online (Sandbox Code Playgroud)
最好的解决方案是引入一个dataAlreadyBeenSetAndWaitingToBeProcessed设置SetData为true 的新布尔变量,并在其结尾处DoLongOperationWith_d设置为true,因此如果SetData调用此变量设置为true,它可能只返回?
我有一些代码来初始化C#中的结构:
namespace Practice
{
public struct Point
{
public int _x;
public int _y;
public int X
{
get { return _x; }
set { _x = value; }
}
public int Y
{
get { return _y; }
set { _y = value; }
}
public Point(int x, int y)
{
_x = x;
_y = y;
}
}
class Practice
{
public static void Main()
{
Point p1;
p1.X = 1;
p1.Y = 2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了编译器错误: …
这个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) 想检查这是否是表示抽象工厂模式的好例子.这是主题戴尔(工厂)制造xps(产品)戴尔(工厂)制造灵感(产品)惠普(工厂)使特使(产品)惠普(工厂)制造presario(产品)
BestBuy销售电脑.
//Abstract factory
abstract class ComputerFactory
{
public abstract Computer BuildComputer(Computer.ComputerType compType);
}
//Concrete factory
class Dell : ComputerFactory
{
public override Computer BuildComputer(Computer.ComputerType compType)
{
if (compType == Computer.ComputerType.xps)
return (new xps());
else if (compType == Computer.ComputerType.inspiron)
return new inspiron();
else
return null;
}
}
//Concrete factory
class Hp : ComputerFactory
{
public override Computer BuildComputer(Computer.ComputerType compType)
{
if (compType == Computer.ComputerType.envoy)
return (new envoy());
else if (compType == Computer.ComputerType.presario)
return new presario();
else
return null;
}
} …Run Code Online (Sandbox Code Playgroud) c# ×10
struct ×2
.net ×1
android ×1
combobox ×1
dispose ×1
encryption ×1
enums ×1
events ×1
finalizer ×1
generics ×1
idisposable ×1
java ×1
locking ×1
overloading ×1
structlayout ×1