IgorShare Thoughts and Ideas

Consulting and Training

Archive for the ‘Visual Studio’ Category

VS 2008 SP1 – the worst user experience ever

Posted by Igor Moochnick on 09/03/2008

A couple of days ago I was seduced by Scott Hansleman to install the VS 2008 SP1. That was a nightmare. The install took me a full day to complete. The first pass has stuck on a stage “Installing .NET Framework 3.5 SP1”. It was stuck, but the process indicator was still spinning. So I had to wait a couple of hours waiting before I decided to kill it and to restart it.

The second pass was better, but still – the user experience is horrible. How hard is it to scroll on the screen ALL the commands that are happening in real time instead of a spinning slash “/|\-“?

After the installation was complete, I’ve received the following very descriptive greeting:

image

So, after all the talks about the improvements in user experience, I’m getting the worst one that ever happened to me.

So far I don’t see any problems with the SP1. So, I guess, it’s safe to use.

Posted in Thoughts, Visual Studio | 1 Comment »

Toolbox for Agile Projects and Developers @ Hartford, CT

Posted by Igor Moochnick on 08/18/2008

image

It was Hartford’s first CodeCamp. I think it was a great success – way to go, Hartford! More to come!

My presentation had a solid attendance and people stayed until the very end. At the end I’ve ran out of time and haven’t had a time to cover in details the Inversion-of-Control and Dependency Injection tools, but, I think, I’ll prepare a separate talk only on this topic – it’s huge and requires a lot of detailed attention.

 

As I’ve promised, I’m publishing the slide deck as well as all the code iterations for you to have some fun:

 

Iteration Introduced Link
Slide deck link
0 Base solution link
1 Unit testing link
2 Source Control & Continuous Integration (CI) link
3 Mocking, Refactoring link
4 IOC (Inversion-of-Control) & DI (Dependency Injection) link

 

Please feel free to contact me for more information or leave your comments on the blog. I’m available to present this topic or any other portion of it in more details on your site – let me know.

Posted in Alt.Net, C#, Community, IOC/DI, Mocking, Presentations, Refactoring, Thoughts, Tutorials, Unit Testing, Visual Studio | Leave a Comment »

Pheww… Two more will not tip the bucket.

Posted by Igor Moochnick on 07/28/2008

MCTS: ADO.Net

MCTS: ASP.Net

This is how it looks like on the paper:

current_certifications

Posted in C#, Community, Thoughts, Visual Studio | Leave a Comment »

70-504 – Congratulations to … me again ?!!!

Posted by Igor Moochnick on 04/19/2008

MCTS_WF

Just got a confirmation that I’ve passed the 70-504 TS: Workflow Foundation.

And, you can guess, I’m heading to a bar for some beers :-) – this becomes a habit and, at this rate, I can become an alcoholic too :lol:

Posted in C#, Presentations, Thoughts, Tutorials, Visual Studio, Workflows | 2 Comments »

Did you know: using ToLower on a Unicode text can cause a security problem?

Posted by Igor Moochnick on 01/26/2008

Recently watched the latest DnrTV show #97 (love them!) with Kathleen Dollard (check her blog Leaning Into Windows). Great show! Kathleen went through a lot of things that are rarely used in the day-to-day projects but, if overlooked, can cause a serious problem and put the whole project/product at risk. Here are 2 that are really worth remembering:

1. In a product that works with Unicode strings (Localized) and do a lot of string comparisons, it’s better to use ToUpper than ToLower. ToUpper will not work correctly for a couple of languages, but ToLower, not only fail for more languages, but, also, can expose a bunch of security problems within Unicode implementation.

2. Do not write ANY code that changes variables in a partial method call. If a partial method implementation will be removed – such code will be removed as well and will cause an unpredictable result. To understand this check out the following code snippet:

   1: partial void PrintNum(int n);
   2:  
   3: public int AddWithIncrement(int a, int b)
   4: {
   5:    PrintNum(a++);
   6:    return a+b;
   7: }

 

Let’s say that a=1 and b=2. If the PrintNum method will have a declaration, then the AddWithIncrement function will return 4. On the other hand, if the declaration of PrintNum will be removed, the compiler will remove (ignore) the whole line #5 (PrintNum(a++)), hence the a++ will not be executed and AddWithIncrement function will return 3.

So, be careful, you have been warned!

Posted in C#, Software, Thoughts, Tutorials, Visual Studio | Leave a Comment »

.NET Source Code is available for public

Posted by Igor Moochnick on 01/17/2008

You can find all the details of how to configure your Visual Studio in a detailed Shawn Burke’s article. It looks like the WPF source can be in there too according to Bart hints on his B#.NET blog.

Posted in Software, Tutorials, Visual Studio | 1 Comment »

CodeDom extensions and dynamic LINQ (string/script to LINQ emitting)

Posted by Igor Moochnick on 01/11/2008

Ah… Don’t you like these method extensions?!!!

Recently I’ve developed a certain taste for method extensions. They allow to create shorter code, but you should always keep in your head the dangers of such freedom – code can become complicated and unnatural. On the other hand, new set of extensions, it’s like to learn a new language, new classes, new techniques. So, currently, I have a mixed feeling – great thing, but not for free (some overhead attached).

In my previous post (Intellisense-like method selection pop-up window) I’ve shown how to create a LINQ editor (or any editor of your choice). You can ask – what’s the point of such script? What can we do with it?

.NET answer – the sky is your limit. The .NET framework allows you dynamically convert any string (correctly formatted) into a code and immediately execute it. You can do this in-memory, in-file, from-file, to-file, from-string, to-string, and any other combination of your choice.

Here are a couple of example of what you can do with a correctly formatted string:

  1. Generate a full code file
  2. Generate an assembly in-memory
  3. Create an assembly in file for future use
  4. etc …

As well you have a wide choice of code providers:

  1. C#
  2. VB.NET
  3. C++
  4. JavaScript
  5. and any other code provider that is installed on your machine and inherits from System.CodeDom.Compiler.CodeDomProvider class.

To simplify the use of CodeDom (it requires a wide knowledge of how the build process works), I’ve created a very basic library that extends CodeDom functionality and provides a nice and slick way of generating code-on-the-fly. Here are a couple of small examples, before we’ll dive into how it’s done (find them in the attached test project):

A) The following snippet prints Hello World! to the console:

   1: var c = new CodeDom();
   2: c.AddNamespace("Samples").Imports("System")
   3:     .AddClass(
   4:       c.Class("TestClass")
   5:         .AddMethod(c.Method("Print", "Console.WriteLine(\"Hello World\");")));
   6: 
   7: var methodPrint = c.Compile().GetType("Samples.TestClass").GetMethod("Print");
   8: methodPrint.Invoke();

B) And here is the example of how to call a function with parametes (it’ll add 2 numbers):

   1: var c = new CodeDom();
   2: c.AddNamespace("Samples").Imports("System")
   3:     .AddClass(
   4:       c.Class("TestClass")
   5:         .AddMethod(c.Method("int", "AddNumbers", "int a, int b", "return a + b;")));
   6: 
   7: var methodAddNumbers = c.Compile().GetType("Samples.TestClass").GetMethod("AddNumbers");
   8: int retVal = Converter.To<int>(methodAddNumbers.Invoke(2, 3), 0);
   9: // retVal = 5

C) and here is the BEST – dynamic LINQ (compiled and executed in memory):

   1: var c = new CodeDom();
   2: c.AddReference(@"System.Core.dll")
   3:     .AddNamespace("Samples")
   4:     .Imports("System.Collections.Generic")
   5:     .Imports("System.Linq")
   6:     .AddClass(
   7:       c.Class("TestClass")
   8:         .AddMethod(c.Method("IEnumerable<string>", "FilterCollection", "IEnumerable<string> items",
   9:              "return from item in items where item.StartsWith(\"a\") select item; ")));
  10: 
  11: var method = c.Compile().GetType("Samples.TestClass").GetMethod("FilterCollection");
  12: 
  13: var items = new List<string>() { "apple", "banana", "ananas", "mango" };
  14: IEnumerable<string> retVal = (IEnumerable<string>)method.Invoke(items);
  15: // retVal contains: "apple", "ananas"

Obviously you can start mix and match these extensions and create more namespaces, classes with more methods, etc … I don’t want to get too much into the details (see attached test project for more samples), but rather I want to tell how it works.

NOTE: Yes, I know about the Dynamic LINQ library and I’ve read ScottGu’s blog article, but, so far, I’ve seen only that you can create filters with this technique. It is using tokenized parser behind the scenes and reflects expression tree based on the results. This has somewhat limited functionality and can’t emit expression tree solely based on a string LINQ query. May be in the future it’ll be expanded, but, at this moment, my solution is the only way to convert LINQ string to an executable code.

Extension methods are the key. CodeDom class just provides a set of factories to create the CodeDom classes like CompileUnit, CodeTypeDeclaration, CodeSnippetTypeMember, etc… It collects all the namespaces, referenced assemblies, constructed code and, at the end, it allows you to decide what is a final product of your actions. The beauty of the code ensured by the extension methods.

I will not go too much into details of how to create extension methods themselves (a lot of information can be found on the net), but rather talk about the most important one: Method.Invoke.

   1: public static object Invoke(this MethodInfo methodInfo, params object[] parameters)
   2: {
   3:     return methodInfo.Invoke(null, parameters);
   4: }

This extension method makes the constructs like “method.Invoke()” and “method.Invoke(2, 3)” possible. It hides the complexity of the reflection from the users and allows him to use the newly constructed method as if it’s a real method of the application.

At the end I want to mention, yet another, beauty of .NET – dynamic type converter. Such converter allows you to  take control over your untyped objects without second-guessing your type conversion assumptions and skipping all the error check logic. I’ve used such technique very extensively with untyped DataSets in all my middle tier applications (I’ll be posting about it soon). For example, Method.Invoke always returns an object. If you want to use the returned value as a strongly typed object – you have to cast it to the required type either by “()” or “as” constructs. Then you need to check for a “null” or an exception. In my case you can do something like this:

   1: int retVal = Converter.To<int>(methodAddNumbers.Invoke(2, 3), 0);

Note the last “0″ in the converter’s call – this is a default value that will be returned in case the conversion has failed. This is how it works:

   1: /// <summary>
   2: /// Converts from "object" general type to a concrete type
   3: /// </summary>
   4: /// <remarks>
   5: /// The class is working with .Net 2.0 and above
   6: /// </remarks>
   7: public class Converter
   8: {
   9:     /// <summary>
  10:     /// Returns True if the type can get Null as a value (is a reference type and not a value one)
  11:     /// </summary>
  12:     public static bool IsNullable(Type t)
  13:     {
  14:         if (!t.IsGenericType) return false;
  15:         Type g = t.GetGenericTypeDefinition();
  16:         return (g.Equals(typeof(Nullable<>)));
  17:     }
  18: 
  19:     /// <summary>
  20:     /// Returns a real type of a first generic argument
  21:     /// </summary>
  22:     private static Type UnderlyingTypeOf(Type t)
  23:     {
  24:         return t.GetGenericArguments()[0];
  25:     }
  26: 
  27:     /// <summary>
  28:     /// Converter
  29:     /// </summary>
  30:     public static T To<T>(object value, T defaultValue)
  31:     {
  32:         if (value == DBNull.Value) return defaultValue;
  33:         Type t = typeof(T);
  34:         if (IsNullable(t))
  35:         {
  36:             if (value == null) return default(T);
  37:             t = UnderlyingTypeOf(t);
  38:         }
  39:         else
  40:         {
  41:             if ((value == null) && (t.IsValueType))
  42:             {
  43:                 return defaultValue;
  44:             }
  45:         }
  46: 
  47:         return (T)Convert.ChangeType(value, t);
  48:     }
  49: }

I’ll be talking about this converter in more detail in the future posts, so stay tuned …

The full source code with tests/examples you can find as usual on my web site: http://igor.moochnick.googlepages.com.

DISCLAMER: The provided libraries are a work in progress and do not contain 100% wrapped CodeDom. There are a lot of things that can be added to make the whole user experience easier. You are welcome to use them AS-IS and to extend them to any degree you with. You’re welcome to send me your updates and I’ll be more than happy to post them for everybody else to use.

Posted in C#, Software, Tutorials, Visual Studio | 9 Comments »

Keep your code professional

Posted by Igor Moochnick on 01/06/2008

Microsoft just released new update to the Spell Checker for VS 2005 and VS 2008.

Read Spell checker update 2.1: a few bug fixes and couple of new features article for more details.

Posted in Software, Visual Studio | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: