You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by Nicko Cadell <ni...@neoworks.com> on 2006/05/01 01:25:21 UTC

RE: Problem when using generic classes

Morten,

The LogManager.GetLogger(Type) method uses the Type.FullName property to
get a string that represents the type name.
In your case the output is correct. 

TestApplication.MyGenericClass`1[[TestApplication.MyClass,
TestApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

This is the correct string representation of the .NET type. As you can
see the syntax is not the same as for generics in C#, but then the .net
runtime supports many languages.
A short C# like syntax could be:

TestApplication.MyGenericClass<TestApplication.MyClass>

Note that the main difference is that the .net type fullname syntax
gives the fully assembly qualified typename for the generic parameters.
As each language has a different type syntax, especially for generic
types it is inappropriate for log4net to select one language syntax over
another.

You can generate this sort of C# style string using the following
function to convert the type object to a string. Use this in your call
to LogManager.GetLogger. 


private static string TypeString(Type t)
{
  if (t.IsGenericTypeDefinition || t.IsGenericType)
  {
    StringBuilder b = new StringBuilder();
    b.Append(t.Namespace).Append('.');

    int offset = t.Name.IndexOf('`');
    if (offset > 0)
    {
      b.Append(t.Name.Substring(0, offset));
    }
    else
    {
      b.Append(t.Name);
    }
    b.Append('<');

    bool first = true;
    foreach (Type param in t.GetGenericArguments())
    {
      if (first)
      {
        first = false;
      }
      else
      {
        b.Append(',');
      }

      if (param.FullName != null)
      {
        b.Append(param.FullName);
      }
      else
      {
        b.Append(param.Name);
      }
    }

    b.Append('>');

    return b.ToString();
  }
  else
  {
    return t.FullName;
  }
}


Hope this helps,
Nicko


> -----Original Message-----
> From: Morten Andersen [mailto:morten@vianett.no] 
> Sent: 30 April 2006 14:25
> To: Log4NET Dev
> Subject: Problem when using generic classes
> 
> I have created a small app to illustrate the problem.
> 
> The output looks like this:
> 2006-04-30 15:19:09,799 [2924 ] [(null)] DEBUG 
> TestApplication.MyGenericClass`1[[TestApplication.MyClass,
> TestApplication, Version=1.0.0.0, Culture=neutral, 
> PublicKeyToken=null]]
> - Hello world
> 
> I was expecting something like this:
> 2006-04-30 15:19:09,799 [2924 ] [(null)] DEBUG 
> TestApplication.MyGenericClass<TestApplication.MyClass> - Hello world
> 
> =================
> namespace TestApplication
> {
>     class MyClass
>     {
>         static void Main()
>         {
>             MyGenericClass<MyClass> test = new 
> MyGenericClass<MyClass>();
>             test.Test();
>         }
>     }
> 
>     class MyGenericClass<T>
>     {
>         static readonly log4net.ILog log = 
> log4net.LogManager.GetLogger(typeof(MyGenericClass<T>));
> 
>         public void Test()
>         {
>             log.Debug("Hello world");
>         }
>     }
> }
> =================
> 
> -- 
> 
> Best Regards
> *Morten Andersen*
> Developer
> Vianett AS <http://www.vianett.no/> | morten@vianett.no 
> <ma...@vianett.no> | Office: +47 69 20 69 74 
> <callto://+4769206974> | Skype: mortander <callto://mortander>
> 
> 
> 

Re: Problem when using generic classes

Posted by Morten Andersen <mo...@vianett.no>.
My guess was that Class<SubClass, SubClass2> is the most common way to 
display a generic class. Anyway. The problem is not the [] instead of 
<>, but the Version, Culture, and PublicKeyToken.

typeof(MyGenericClass<MyClass, TestClass>).ToString()

result:

TestApplication.MyGenericClass`2[TestApplication.MyClass,TestApplication.TestClass]


- Morten

Nicko Cadell wrote:
> Morten,
>
> The LogManager.GetLogger(Type) method uses the Type.FullName property to
> get a string that represents the type name.
> In your case the output is correct. 
>
> TestApplication.MyGenericClass`1[[TestApplication.MyClass,
> TestApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
>