我试图用旧版本的应用程序反序列化"SomeClass".我得到以下异常
System.Runtime.Serialization.SerializationException:ObjectManager发现无效的修正次数.这通常表示Formatter中存在问题.
当序列化版本0.9并尝试使用版本0.8反序列化时,反序列化会抛出异常.我认为该OptionalField属性可以解决问题,但事实并非如此.
// Version 0.8
[Serializable()]
class Foo{
Bar b;
}
// Version 0.9
[Serializable()]
class Foo{
Bar b;
[OptionalField]
Zoo z;
}
Run Code Online (Sandbox Code Playgroud)
鉴于我无法更改版本0.8,我应该如何向Foo对象添加更多状态,以便以前的版本可以反序列化它们的任何内容?
任何指针都会非常感激.
Update 1 Bar和Zoo是其他可序列化的类,包含Hashtables和其他可序列化的东西.在这些课程中,一切都是可序列化的.另外,我没有任何支柱.
我可以在方法中定义后台工作程序吗?
private void DownLoadFile(string fileLocation){
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler((obj, args) => {
// Will be executed by back ground thread asynchronously.
args.Result = Download(fileLocation);
});
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((obj, args) => {
// will be executed in the main thread.
Result r = args.Result as Result;
ReportResult(r);
});
worker.RunWorkerAsync(fileLocation);
}
Run Code Online (Sandbox Code Playgroud)
问题:如果Download()函数需要很长时间才能下载文件,那么在执行RunWorkerCompleted()之前,GC是否可以启动并收集工作对象?
// [in designer] textBoxInContext.AutoCompleteMode = Suggest
// [in designer] textBoxInContext.AutoCompleteSource = CustomSource
AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange(myArrayofStrings);
textBoxInContext.AutoCompleteCustomSource = autoComplete;
Run Code Online (Sandbox Code Playgroud)
我有这个代码,它运行良好,如MSDN中所述.
问题:如果用户键入"PS",则显示以"PS"开头的所有字符串; 我想显示包含"PS"的所有字符串
有什么指针吗?
Winforms .NET 3.5(C#)
我有一个DataGridView(DGView),我创建了CustomColumn和CustomCell以显示在DGView中.我创建了一个CustomUserControl,我想在CustomCell中显示它.
问题:我没有在列中看到用户控件.我想我需要在CustomCell中覆盖Paint()方法 - 任何一点我该怎么做?
注 - 托管用户控件的MSDN示例用于编辑单元格值 - 您可以在其中使用户控件在您编辑单元格的位置可见.我希望我的用户控件呈现为正常的winform控件.此用户控件显示行的通知..每行可以有不同的通知.我希望用户能够点击通知并获得有关它的更多详细信息...但是现在我被困在"如何显示这个用户控件"
任何指针都将受到高度赞赏.
public class CustomColumn : DataGridViewColumn {
public CustomColumn() : base(new CustomeCell()) { }
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CustomeCell)))
{
throw new InvalidCastException("It should be a custom Cell");
}
base.CellTemplate = value;
}
}
}
public class CustomeCell : DataGridViewTextBoxCell
{
public CustomeCell() …Run Code Online (Sandbox Code Playgroud) 示例Outlook:它只有一个进程,但可以有多个窗口(用户可以双击电子邮件在自己的窗口中打开它)
所以,我不能使用Process.GetProcess()迭代进程并比较标题.
FindWindow()需要完整的标题; 我知道标题,但Outlook窗口最后添加了自己的标题,即 - 消息(富文本)或 - 消息(HTML)..等.
我可以获取Outlook的所有电子邮件窗口(句柄)吗?然后迭代那些使用GetWindowText()找到我的窗口?任何指针都将受到高度赞赏.
多次讨论从工作线程调用UI线程,我们知道为什么要使用BeginInvoke()而不是Invoke().我最近发布了这个问题,在做了一些研究之后,我发现在UI线程上调用(异步)某些东西至少有三种不同的方式(内部可能是相同的).
Control.BeginInvoke()SynchronizatoinContext类Dispatcher.BeginInvoke(priority.. )任何人都可以告诉我哪种方式可以异步调用在UI线程上执行的方法.有经验吗?我看到Dispatcher.BeginInvoke有优先级组件,是否使它更可靠?
上下文:
我们正在使用someControl.BeginInvoke()但注意到有时(不幸的是只在最终用户生产环境中)委托传递给BeginInvoke is从未执行过,这让我相信它创建的帖子消息会丢失.我们想要一种可靠的方式来回传到UI线程.control.Invoke()有时挂起UI,所以我们也不想去那里.
场景:我们有一个附加到DataAdapter(datatable)的DataGridView,我们在一个单独的线程中使用(adapter.fill(query,datatable))在datatable中加载数据(使用delegate和beginInvoke),一旦加载了数据,我们附加了数据表到datagridview(在主线程中)
有没有办法可以检查fill()是否仍在执行并取消它.
真实场景:用户点击用户名,相应的数据将加载到数据网格中.有时,用户不耐烦并点击另一个用户(这里我要取消之前的填充并开始新的填充)
更新:我们保留两个DataApdater(和两个DataTables),我们将一个数据表附加到datagridview,并开始异步地将数据加载到另一个数据表.加载数据时,我们只需将datagridview绑定到我们刚刚填充的DataTable(并开始异步加载以前的数据)这样UI将始终获取当前数据(无需用户在UI上等待刷新或挂起)
我知道这已经讨论过了,但我找不到满意的答案.
我有一个电子邮件文件(.msg),我打开如下,然后调用显示.
oApp = new Microsoft.Office.Interop.Outlook.Application();
mail = (Microsoft.Office.Interop.Outlook.MailItem)oApp.Session.OpenSharedItem(fileName);
mail.Display(false);
oApp = null; // do I need to release this object ?
Run Code Online (Sandbox Code Playgroud)
用户可以关闭它并重新打开它.在他们点击"重新打开"之前,我检查窗口是否仍然存在,如果是,那么只需将SetForeground(hwnd)发送到该窗口.如果不是,这意味着用户关闭它,所以只需释放mailItem对象并再次打开.
public static void ReleaseCOMObject(Microsoft.Office.Interop.Outlook.MailItem item) {
int r = System.Runtime.InteropServices.Marshal.ReleaseComObject(item);
while (r != 0) {
r = System.Runtime.InteropServices.Marshal.ReleaseComObject(item);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我再次尝试打开同一个文件.它"有时"会抛出"正在使用的文件异常"所以,我认为即使我发布了MailItem,它也没有被正确发布.
我该怎么做才能确保其正确发布.关闭和重新打开文件是一种非常常见的情况.
任何指针都会非常有用.
// No overrides required .. let CLR take care of equal and hashcode.
Class Foo {public Name{get; set;} public Address{get; set;}}
Dictionary<List<Foo>, int> map = new Dictionary<List<Foo>, int>();
Run Code Online (Sandbox Code Playgroud)
题:
这段代码看起来不错吗?我知道要成为Map中的一个键,Foo需要覆盖equals和hashcode方法 - 要么覆盖两者,要么不覆盖.
我想知道对象列表作为键是什么?当涉及到List时,平等意味着什么?上面定义的地图是否可以安全地避免"对象丢失在地图中"问题?
-Karephul
我想使用C#代码保存TextPad窗口; 我可以找到窗口的句柄但不知道如何将CTRL-S发送到该窗口.我想使用P/Invoke API来实现这一点.此外,该TextPad窗口将处于非活动状态,因为此时我的应用程序将处于活动状态.
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
Run Code Online (Sandbox Code Playgroud)
我看了一下这个与我的问题非常相似的讨论.我逻辑上理解我必须做4发送消息,如下所示
我不知道如何将正确的参数发送到SendMessage.关闭我使用的窗口
SendMessage(hWnd, 0x0010, 0, 0);
Run Code Online (Sandbox Code Playgroud)
我从MSDN库得到了这个.
你能指点我一些链接告诉我键盘上键的十六进制,并解释最后两个参数代表什么?
更新 - 1
使用spy ++我发现这些事件是我在记事本窗口按CTRL-S生成的
1. WM_KEYDOWN nVirtKey:VK_Control, 2. WM_KEYDOWN nVirtKey:'S' .. some other messates .. 3. WM_KEYUP nVirtKey:VK_Control. 4. WM_KEYUP nVirtKey:'S'.
更新 - 2
private IntPtr startnotepad() {
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"notepad.exe";
String fileName = baseDirectory + textbox1.Text;
psi.Arguments = …Run Code Online (Sandbox Code Playgroud)