Private Accessor class ignores generic constraint

Keine Kommentare »

These days, i came across a problem with Team System Unit Testing. I found that the automatically created accessor class ignores generic constraints - at least in the following case:

Assume you have the following class:

namespace MyLibrary
{
  public class MyClass
  {
    public Nullable<T> MyMethod<T>(string s) where T : struct
    {
      return (T)Enum.Parse(typeof(T), s, true);
    }
  }
}

If you want to test MyMethod, you can create a test project with the following test method:

public enum TestEnum { Item1, Item2, Item3 }

[TestMethod()]
public void MyMethodTest()
{
  MyClass c = new MyClass();
  PrivateObject po = new PrivateObject(c);
  MyClass_Accessor target = new MyClass_Accessor(po);

  // The following line produces the following error:
  // Unit Test Adapter threw exception: GenericArguments[0], 'T', on
  // 'System.Nullable`1[T]' violates the constraint of type parameter 'T'..
  TestEnum? e1 = target.MyMethod("item2");

  // The following line works great but does not work for testing private methods.
  TestEnum? e2 = c.MyMethod("item2");
}

Running the test will fail with the error mentioned in the comment of the snippet above. The problem is the accessor class created by Visual Studio. If you go into it, you will come up to the following code:

namespace MyLibrary
{
  [Shadowing("MyLibrary.MyClass")]
  public class MyClass_Accessor : BaseShadow
  {
    protected static PrivateType m_privateType;

    [Shadowing(".ctor@0")]
    public MyClass_Accessor();
    public MyClass_Accessor(PrivateObject __p1);
    public static PrivateType ShadowedType { get; }
    public static MyClass_Accessor AttachShadow(object __p1);

    [Shadowing("MyMethod@1")]
    public T? MyMethod(string s);
  }
}

As you can see, there is no constraint for the generic type parameter of the MyMethod method.

Is that a bug? Is that by design? Who knows how to work around that problem?

Feel free to download the Visual Studio 2008 Solution that demonstrates the problem in the Download Area.

Volta

, , Keine Kommentare »

VoltaThe Volta Tech Preview has arrived. Volta is the knife and glue to split your .NET applications into several tiers and let them work seamlessly together afterwards.

 

That's what Microsoft states about Volta:

The Volta technology preview is a developer toolset that enables you to build multi-tier web applications by applying familiar techniques and patterns. First, design and build your application as a .NET client application, then assign the portions of the application to run on the server and the client tiers late in the development process. The compiler creates cross-browser JavaScript for the client tier, web services for the server tier, and communication, serialization, synchronization, security, and other boilerplate code to tie the tiers together.

Microsoft Codename “Astoria”

, , Keine Kommentare »

At Mix 07, Microsoft announced another interesting project.

The goal of Microsoft Codename Astoria is to enable applications to expose data as a data service that can be consumed by web clients within a corporate network and across the internet. The data service is reachable over HTTP, and URIs are used to identify the various pieces of information available through the service. Interactions with the data service happens in terms of HTTP verbs such as GET, POST, PUT and DELETE, and the data exchanged in those interactions is represented in simple formats such as XML and JSON.

So keep on watching, keep on learning!

ForEach-ing only certain objects in a list

, , 1 Kommentar »

Jeremy from Digital Blasphemy brought up the question of how to iterate only over certain objects in a list. In his example, he has a list filled with Cats and Dogs where both are inherited from the base class Animal. What he wanted to do is to use a foreach statement to iterate over the Dogs only without any kind of filtering inside the foreach loop. I pointed out that this is easy to achieve using an anonymous delegate as the predicate in the FindAll method of the List type. The code looks like that:

List<Animal> animals = new List<Animal>();
animals.Add(new Cat("Fluffy"));
animals.Add(new Dog("Spot"));
animals.Add(new Dog("Lucky"));
animals.Add(new Cat("Frisky"));
animals.Add(new Dog("Fido"));
foreach (Dog dog in animals.FindAll(delegate(Animal a) {return a is Dog;}))
{
  Console.WriteLine("Found a dog named " + dog.Name);
}

Micro-soft

, Keine Kommentare »

has released the brand new .Net Micro Framework.

The Microsoft .NET Micro Framework is an environment that extends the advantages of Microsoft .NET and the toolset in the Microsoft Visual Studio development system into a class of smaller, less expensive, and more resource-constrained devices than previously possible with other Microsoft embedded offerings.

Let's start playing around.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Anmelden