Search code examples
c#unit-testinggenericsmicrosoft-fakes

MSFakes Naming Convention to match Generic class?


I have a class called MyClass, and a generic version called MyClass<C>. Using fakes, I want to generate a shim of JUST MyClass<C>, and not MyClass. I originally tried:

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
  <Assembly Name="XXXX" Version="#.##.###"/>
  <ShimGeneration>
    <Clear/>
    <Add FullName="MyClass!"/>
  </ShimGeneration>
</Fakes>

This generated shims for MyClass, but not MyClass<C>. If I change the ! to a *, it does match MyClass<C>. This leads me to believe there is some naming convention I need to use to match MyClass<C>. Does anyone know what it is/where I could find out?


Solution

  • You use the grave accent character plus the number of generic type parameters on the type. For example: MyClass<T> would be 1, MyClass<T,U> would be 2, etc. See also CLI specification Section 10.7.2 Type names and arity coding.

    You should also add your namespace as a part of the value.

    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
      <Assembly Name="XXXX" Version="#.##.###"/>
      <ShimGeneration>
        <Clear/>
        <Add FullName="MyNamespace.MyClass`1!"/>
      </ShimGeneration>
    </Fakes>