C# Language (RSS)

The one and only language of all cutting-edge .NET developers.

Creating thread safe code with C# and .NET

If you have developed traditional Windows Client/Server applications on single-CPU machines for all your life the development of thread-safe code is probably something you did not had to bother.

Here are a few interesting links on how to do it correctly:

Further must-reads? Leave a comment, please!

with 1 Comments

UI Testing with Messages or Active Accessibility: What comes next?

Look at this post by Randy Miller how the C# test team tested Visual C# 2005.

TAO: Test Automation Object
An important part of what a QA team does is automating test scenarios, many of which require manipulating the user interface. Over time we have created internal tools based mostly on Active Accessibility to do so. A common and significant problem with these tools is synchronizing the tests with the target application; writing very robust tests using this method has not always been easy. When the product UI changes, it often breaks tests and simple focus issues on the test machine can cause false positives.
Trying to circumvent this problem the C# test team created TAO (Test Automation Object).  TAO is basically a test object in the language services that is only instantiated in a test environment.  [...]

Interesting approach.

with 0 Comments

Controls sometimes disappear or reposition without your consent in Visual C#?

Do the symptoms in the title sound familiar to you? There are hotfixes available!

Microsoft has released three knowledge base articles - including patches - recently:

However, if you want to have these patches you have contact Microsoft. I wonder why they are not made publically available...

with 2 Comments

Different property accessors for getter and setter in C# 2.0

It's not a big thing though most articles showing the new features in C# 2.0 tend to skip it - most likely because of the fact it was not yet implemented in the PDC bits.

In C# 2.0 you will finally be able to set the visibility for the getter and setter separatly with writing code like this:

  public string Text
  {
     get
     {
        return "Hello World";
     }
     internal set
    
{
        // do something
    
}
  }


In C# 1.0 you had to write additional code (another property or method) to be able to declare a property readonly for the public and read/write for internal access.

I'm really looking forward to start productive work with C# 2.0, don't you agree?

with 0 Comments

Invoking private members from any class

Did you know... ?

... that access modifiers like public, private and internal are enforced by the language compilers (C#, VB.NET) only and not on IL level?

That means that you can invoke any private or internal member in any class, residing in any assembly, just by using Reflection.

To invoke a private static method Calc from the internal class Calculations in the AssemblyWithPrivateMembers assembly that requires two parameters of type int and returns an int, you can write the following few lines of code.

  Assembly asm = Assembly.Load("AssemblyWithPrivateMembers");

  Type t = asm.GetType("AssemblyWithPrivateMembers.Calculations");

  object[] args = new object[] { 2, 4 };

  BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod;

  int result = (int)(t.InvokeMember("Calc", bindingFlags, null, null, args));

For you as a developer it means that you can use all non-public members you find by browsing the .NET framework base class library (using .NET Reflector for example).

But: Please use this possibility with caution! There will always be a reason why a class or member is not declared public. Additionally you are never sure if the class or member will change in later versions of the assembly.

Enjoy!

with 0 Comments

Iterator Isolation and Filter Iterators in C#

If you have not started using the iterators created by Eric Gunnerson in his Tips & Tricks article published on MSDN beginning 2002 then you should definetly do this right now.

Finally, it's possible to delete items from the collection, you're currently enumerating with for each using the following code:

  foreach (string s in new IterIsolate(hash.Keys))
  {
    if ((int) hash[s] == 0)
      hash.Remove(s);
  }

And that's just the beginning...

How about iterating though a collection that just returns items you approved in a custom filter function? Watch this:

  public static bool MyFilter(object o)
  {
    if ((string) o == "A")
      return true;
    else
      return false; 
  }

  IterSelectDelegate selector = new IterSelectDelegate(MyFilter);
  foreach (string s in new IterSelect(test, selector))
  {

     ...
  }
}

You can download the latest version with a new cool iterators that are not mentioned in Eric's MSDN article here.

Enjoy!

with 0 Comments