…meie igapäevast IT’d anna meile igapäev…


Microsoft Exam 70-483: Programming in C#

Filed under: Programmeerimine — Sander @ 10:44:40
Tags: , , ,


Yesterday I passed MS exam 70-483: Programming in C#. It was not an exam I really needed for MS Visual Studio 2012 certifications, as I already have MCPD (VS 2010 certifications) for both Windows and Web – so I could have done just the two exams required for an upgrade. However, I wanted to ensure I am properly up to date with .NET 4.5.

As of now, there really aren’t any “official” study materials. Microsoft’s Exam Ref 70-483: Programming in C# will come out in April 2013, Wrox’s MCSD Certification Toolkit (Exam 70-483): Programming in C# won’t be out until May 2013. Andreas Hammar has collected study links for the exam in his blog.

I read Pro C# 5.0 and the .NET 4.5 Framework, skipping quite a few sections as it is a very basic introduction (and the attempted humor in the book is just bad). Jeffrey Richter’s excellent CLR via C#, 4th Edition was both more interesting and in-depth – I read only Part II: Designing Types and  Part III: Essential Types for the exam, rest will be my Christmas reading (hey, to each his own!).

The exam itself was easy – probably easiest out of all eight Microsoft exams I’ve done so far. That wasn’t really surprising, as I am working daily with those areas covered in the exam. Maybe I should have paid more attention to performance counters, but despite Microsoft pushing those heavily, next to no one uses performance counters, opting for various other profilers instead.

However, what was surprising was how bad the exam questions were. Yes, that is a new, fresh exam – but that is not an excuse for sloppiness. Obviously I cannot go into details of the questions, but several of the questions were ambiguous – i.e. they did not cover all the requirements. Formatting of the code sometimes skipped spaces, i.e. instead of int i there was inti and so forth.

At least one question promoted just horrible way of coding. Yes, the problems are artificially constructed, but whoever prepared the exam should realize a lot of newbie programmers will see the code and think this is the way that particular casting operation should be done – and they will use it in their production code, causing bad performance and hard-to-find bugs.

And even worse, one question about nullable types and LINQ did not have a simplest and most logical answer. Again, this is something I have been working with daily for years, often implementing complex business logic.

I left quite a few comments to the exam review, but unfortunately, I really doubt anyone will actually read those and fix the issues. Which is a shame, as exams (same as study books) should always promote best practices and clean code.


C#: kill process by name for the current user

Filed under: Programmeerimine — Sander @ 09:59:20
Tags: ,

Quite often there is a need to kill a specific process or several processes. While processes are easy to get with Process.GetProcessesByName(), Process class doesn’t give us the owner of the process, so we will invariably get processes for all users. We have to turn to WMI to get the owner of the process:

  1. ///<summary>
  2. /// Kill the processes specified by the name (“notepad.exe”)
  3. ///</summary>
  4. ///<param name=”processName”>Name of the process, case-insensitive.</param>
  5. ///<param name=”currentUserOnly”>If true, kill only processes by current user</param>
  6. public static void KillProcessByName(string processName, bool currentUserOnly = true)
  7. {
  8.     try
  9.     {
  10.         string userName = null;
  11.         if (currentUserOnly)
  12.         {
  13.             WindowsIdentity user = WindowsIdentity.GetCurrent();
  14.             if (user == null)
  15.                 throw new InvalidCredentialException(“No current user?!”);
  16.             userName = user.Name;
  17.         }
  18.         var processFinder = new ManagementObjectSearcher(string.Format(“Select * from Win32_Process where Name='{0}‘”, processName));
  19.         var processes = processFinder.Get();
  20.         if (processes.Count == 0)
  21.             return;
  22.         foreach (ManagementObject managementObject in processes)
  23.         {
  24.             var pId = Convert.ToInt32(managementObject[“ProcessId”]);
  25.             var process = Process.GetProcessById(pId);
  26.             if (currentUserOnly) //current user
  27.             {
  28.                 var processOwnerInfo = new object[2];
  29.                 managementObject.InvokeMethod(“GetOwner”, processOwnerInfo);
  30.                 var processOwner = (string)processOwnerInfo[0];
  31.                 var net = (string)processOwnerInfo[1];
  32.                 if (!string.IsNullOrEmpty(net))
  33.                     processOwner = string.Format(“{0}\\{1}”, net, processOwner);
  34.                 if (string.CompareOrdinal(processOwner, userName) == 0)
  35.                     process.Kill();
  36.             }
  37.             else //any user                    
  38.                 process.Kill();
  39.         }
  40.     }
  41.     catch (Exception ex)
  42.     {
  43.         //There is a good chance for UnauthorizedAccessException here, so
  44.         //log the error or handle otherwise
  45.     }
  46. }

You can easily modify the method not to work only with current user, but any user you specify – just make userName into a parameter. Or you can use this to return information about user having processes with that name – don’t use it as a replacement for Mutex or EventWaitHandle, though.


Get all constant values of a class using reflection

Filed under: Programmeerimine — Sander @ 18:38:23
Tags: , ,
  1. /// <summary>
  2. /// Return all the values of constants of the specified type
  3. /// </summary>
  4. /// <typeparam name="T">What type of constants to return</typeparam>
  5. /// <param name="type">Type to examine</param>
  6. /// <returns>List of constant values</returns>
  7. public static List<T> GetConstantValues<T>(Type type)
  8. {
  9.     FieldInfo[] fields = type.GetFields(BindingFlags.Public
  10.         | BindingFlags.Static
  11.         | BindingFlags.FlattenHierarchy);
  13.     return (fields.Where(fieldInfo => fieldInfo.IsLiteral
  14.         && !fieldInfo.IsInitOnly
  15.         && fieldInfo.FieldType == typeof(T)).Select(fi => (T)fi.GetRawConstantValue())).ToList();
  16. }

Above is a simple generic method to get all constants of a specific type of a class. I.e. to get all string constants, use:

var allStringConstants = GetConstantValues<string>(typeof(myClass));

To get all int values:

var allIntConstants = GetConstantValues<int>(typeof(myClass));

and so forth.

The inspiration for this method came from http://weblogs.asp.net/whaggard/archive/2003/02/20/2708.aspx

Järgmine lehekülg »

The Rubric Theme. Create a free website or blog at WordPress.com.


Get every new post delivered to your Inbox.

Join 84 other followers