Array.ForEach(Enum.GetNames(typeof(DayOfWeek)), Console.WriteLine);
Development Notes
woensdag 5 januari 2011
C# - 1 line to list all names of an enum
maandag 3 januari 2011
C# - using the Uri class to find the delta of 2 paths
foreach (string file in Directory.GetFiles(<directory>, <searchpattern>, SearchOption.AllDirectories)) { Uri uriFile = new Uri(Directory.GetCurrentDirectory()).MakeRelativeUri(new Uri(file)); Console.WriteLine(Path.GetDirectoryName(uriFile.ToString())); }
woensdag 15 december 2010
C# - create an instance of any non-static class
using System.Runtime.Serialization;
/// <summary> /// Gets an uninitialized instance of type T /// </summary> /// <typeparam name="T">Type of object to create an uninitialized instance from</typeparam> /// <returns>Uninitialized instance of type T</returns> /// <remarks>BEWARE: no constructors are called when creating the instance of type T, it will be uninitialized and invalid</remarks> public static T GetUninitializedObject<T>() where T : class { return FormatterServices.GetUninitializedObject(typeof(T)) as T; }
nunit - ignore a test at runtime
dinsdag 14 december 2010
C# - generate and start a process (EXE) on the fly
public static Process GenerateRuntimeProcess(string processName, int aliveDuration, bool throwOnException = true) { Process result = null; try { AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName() { Name = processName }, AssemblyBuilderAccess.Save); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(processName, processName + ".EXE"); TypeBuilder typeBuilder = moduleBuilder.DefineType("Program", TypeAttributes.Public); MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, null, null); ILGenerator il = methodBuilder.GetILGenerator(); il.UsingNamespace("System.Threading"); il.EmitWriteLine("Hello World"); il.Emit(OpCodes.Ldc_I4, aliveDuration); il.Emit(OpCodes.Call, typeof(Thread).GetMethod("Sleep", new Type[] { typeof(int) })); il.Emit(OpCodes.Ret); typeBuilder.CreateType(); assemblyBuilder.SetEntryPoint(methodBuilder.GetBaseDefinition(), PEFileKinds.ConsoleApplication); assemblyBuilder.Save(processName + ".EXE", PortableExecutableKinds.Required32Bit, ImageFileMachine.I386); result = Process.Start(new ProcessStartInfo(processName + ".EXE") { WindowStyle = ProcessWindowStyle.Hidden }); } catch { if (throwOnException) { throw; } result = null; } return result; }
To view IL code of existing .NET code use ildasm.exe, here is a nice example of how to use ildasm.exe from within Visual Studio.
C# - just 1 PInvoke to see if some window is fullscreen
1 Find the process (Process.Get...)
2 Get the process window area (PInvoke GetWindowRect)
3 Get the screen the process window is on (Screen.FromHandle)
4 Assert if the screen working area equals the process window area
But beware, toolbars and the taskbar affect the screen working area. The screen working area equals the screen bounds if no toolbars are present and the taskbar is on autohide. To see if the taskbar is on autohide or toggle autohide use the Windows 7 Code Pack.
And here's a short example:
if (process != null && !process.HasExited) { RECT rect = new RECT(); if (GetWindowRect(new HandleRef(process, process.MainWindowHandle), out rect)) { windowArea = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); screenArea = Screen.FromHandle(process.MainWindowHandle).WorkingArea; result = windowArea.Equals(screenArea); logger.Trace("Window area [" + windowArea + "] " + (result ? "equals" : "does NOT equal") + " screen area [" + screenArea + "]"); } else { logger.DebugException("Failed to obtain window area for process with exception", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())); } }
vrijdag 10 december 2010
C# - remoting on dual-homed PCs
Dual-homed PCs are PCs with 2 NICs (a NIC is a Network Interface Connector
or network card). 1 NIC is configured for the LAN (with one or more private IPs).
is a catch: Remoting gets lost when trying to handshake with the source of the
According to Microsoft documentation remoting can be bound to a specific IP
using the bindTo property. But for TCP remoting this doesn't work, it just generates a socket
C# - remoting and serialization
The following I copied from an old blog somewhere else.
Ever thought of using remoting to distribute your objects?
Ever thought of using serialization to persist your objects?
Ever thought of combining the two?
You might want to read this first.
To use your objects for remoting you need to make them inherit from MarshalByRefObject.
That's perfectly serializable as long as your objects are not used accross application domain boundaries.
In other words, that's perfectly serializable as long as you don't use them for remoting (how convenient!).
Because if you ever do, the private MarshalByRefObject.ServerIdentity gets set. And it isn't serializable.
There are articles on this issue all over the internet. In fact, Microsoft acknowledges the issue but refuses to fix it. If you want to read more try reading the article titled "Serialization of MarshalByRefObject fails after a remoting identity has been set" here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=97623
A lot of articles on this issue focus on bringing a fix for the issue. I feel they might be missing the obvious. Persistence is about preserving an object's state at a certain moment in time. But does it have to be instantaneous?
So persisting an object used for remoting fails. But persisting an object not used for remoting succeeds. That may sound pretty useless, but remember, your object initially hasn't been used for remoting yet. So if you only ever had to persist objects not used for remoting yet you'd be fine.
Let's do that. Add a copy constructor to your class that inherits from MarshalByRefObject and needs persistence. If you need to persist such an object call the copy constructor to create a new one and use that object for serialization. MarshalByRefObject.ServerIdentity hasn't been set on the new object yet because it hasn't been used for remoting yet.
Fixed.
C++ - see if a file exists without opening it
Opening files can have quite an effect on performance. So if you just want to know if a file is there you might not want to open it. Instead you could use the _access method to see if a file exists. More information can be found on MSDN here http://msdn.microsoft.com/en-us/library/1w06ktdy(v=VS.71).aspx
If the method returns a -1 and errno is set to ENOENT then the file does not exist. Set the mode parameter of the _access method to 0 to check for existence only.
C# - easily get the message of a windows error code
string win32ErrorMessage = new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error()).Message;