c#-2.0

calling managed c# functions from unmanaged c++

烈酒焚心 提交于 2019-11-28 05:59:22
How to call managed c# functions from unmanaged c++ Or use a project of mine that allows C# to create unmanaged exports. Those can be consumed as if they were written in a native language. I used COM interop first, but by now I switched to IJW (it just works), as it is a lot simpler. I have a wrapper C++/CLR DLL (compile with /clr). A simple example (using statics to make the calls easier): namespace MyClasses { public class MyClass { public static void DoSomething() { MessageBox.Show("Hello World"); } } } In the DLL I can reference namespaces as follows: using namespace MyClasses; And call it

How to convert Dictionary<string, object> to Dictionary<string, string> in c#

依然范特西╮ 提交于 2019-11-28 05:37:32
I have below code in C# Dictionary<string, object> dObject = new Dictionary<string, object>(); I want to convert dObject to Dictionary<string, string> . How can I do this? Use the ToDictionary method: Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value.ToString()); Here you reuse the key from the original dictionary and you convert the values to strings using the ToString method. If your dictionary can contain null values you should add a null check before performing the ToString: Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value

Forwarding events in C#

痴心易碎 提交于 2019-11-28 04:50:59
I'm using a class that forwards events in C#. I was wondering if there's a way of doing it that requires less code overhead. Here's an example of what I have so far. class A { public event EventType EventA; } class B { A m_A = new A(); public event EventType EventB; public B() { m_A.EventA += OnEventA; } public void OnEventA() { if( EventB ) { EventB(); } } } Class A raises the original event. Class B forwards it as EventB (which is essentially the same event). Class A is hidden from other modules so they can't subscribe to EventA directly. What I'm trying to do is reduce the code overhead in

Get hardware Info

北城以北 提交于 2019-11-28 04:41:57
问题 How to get hardware information of a system using c# code? 回答1: You can use the System.Management namespace for retrieving the hardware information of a machine using C#. Here's an article for retrieving hardware information in C#. And here's a list of WMI classes. 来源: https://stackoverflow.com/questions/908902/get-hardware-info

Exception during iteration on collection and remove items from that collection [duplicate]

放肆的年华 提交于 2019-11-28 01:08:44
问题 This question already has an answer here: What is the best way to modify a list in a 'foreach' loop? 11 answers I remove item from ArrayList in foreach loop and get follwing exception. Collection was modified; enumeration operation may not execute. How can I remove items in foreach, EDIT: There might be one item to remove or two or all. Following is my code: /* * Need to remove all items from 'attachementsFielPath' which does not exist in names array. */ try { string attachmentFileNames =

How to insert null into database?

℡╲_俬逩灬. 提交于 2019-11-28 00:21:15
问题 Hi I am trying to insert null in a database column depending on a gridview datakeys value (if being "" insert null into database) However, I am getting a space ' ' inside the database column. string sbcId = gvTest.DataKeys[gr.RowIndex]["myColumn"].ToString(); insgnp.Parameters.Add(new OleDbParameter("EMPID", (sbcId==""?DBNull.Value.ToString():sbcId))); 回答1: You have to rewrite your code: if(string.IsNullOrEmpty(sbcId)) Parameters.Add(new OleDbParameter("EMPID", DBNull.Value)); else Parameters

how to close a running instance of Word document? (C#)

此生再无相见时 提交于 2019-11-27 22:29:08
I could see a lot of very similar threads all around, but nothing seem to give me a solution which ought to be very basic. From my winforms application, I need to close a running instance of a word document (opened from the application itself). When I open the word document from the application, I keep a track of it in a list. Now how can I close the same doc? Here is what I tried: private bool CloseWord(string osPath) //here I pass the fully qualified path of the file { try { Word.Application app = (Word.Application)Marshal.GetActiveObject("Word.Application"); if (app == null) return true;

Faking browser request in ASP.net C#

与世无争的帅哥 提交于 2019-11-27 21:23:26
问题 I'm using the code below to pull one of our 3rd party developed pages in so I can parse it as XML for my random bits of work. Irritatingly we stil have a browser detection level set on the server that only allows certain browsers on to the site; so the question is how would I fake it so that the server thinks its a browser request? static string GetHtmlPage(string strURL) { String strResult; System.Net.WebResponse objResponse; System.Net.WebRequest objRequest = System.Net.HttpWebRequest

Function evaluation disabled because a previous function evaluation timed out

。_饼干妹妹 提交于 2019-11-27 20:59:08
问题 I have an C# application in which I am getting this error : "Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation." I saw many posts related to this error on stackoverflow and on msdn also but found no solution. Most of the people say that this error comes in multithreaded application and can be resolved by deleting all the breakpoints. In my case, my app is single threaded and I have deleted all the

C# ?: Conditional Operator

江枫思渺然 提交于 2019-11-27 19:59:09
I have this extract of C# 2.0 source code: object valueFromDatabase; decimal result; valueFromDatabase = DBNull.Value; result = (decimal)(valueFromDatabase != DBNull.Value ? valueFromDatabase : 0); result = (valueFromDatabase != DBNull.Value ? (decimal)valueFromDatabase : (decimal)0); The first result evaluation throws an InvalidCastException whereas the second one does not. What is the difference between these two? Eric Lippert UPDATE: This question was the subject of my blog on May 27th 2010 . Thanks for the great question! There are a great many very confusing answers here. Let me try to