I filed a ticket on this exact issue but apparently the approach of
Mono.Cecil is to expose an API that appears to mimic the underlying
metadata in the assembly instead of how types are logically
represented from a developer's perspective. Here's how I worked around
it:

public static string GetNamespace(TypeReference type) {
        if (type == null)
                throw new ArgumentNullException("type");

        string ret = null;
        var nextType = type;
        while (nextType != null) {
                ret = nextType.Namespace;
                nextType = nextType.DeclaringType;
        }
        return ret;
}

On Feb 21, 12:45 am, "Wee Li Yen" <[email protected]> wrote:
> I tried to get the Namespace of types, and found that the namespace for 
> Struct or inner Class is empty.
> This is causing a problem when I want to do analysis on a bigger project.
> Is there a problem with Cecil or?
>
> Thanks,
> LY
>
> --------------------------------------------------------------------------- 
> ----------------------------------------------------------------
> This is the following output I got...
> *************************************************************************** 
> ************
> Namespace: TestTest
> Type: TestTest.Test
>
> Namespace:
> Type: TestTest.Test/innerClassTest
>
> Namespace:
> Type: TestTest.Test/structTest
>
> *************************************************************************** 
> ************
> ...using the piece of code below...
> *************************************************************************** 
> ************
>
>         public override void 
> VisitTypeDefinitionCollection(TypeDefinitionCollection types)
>         {
>             foreach (TypeDefinition item in types)
>             {
>                 if (item.Name != "<Module>")                
>                 {
>                      MessageBox.Show("Namespace: " + item.Namespace + 
> "\nType: " + item.FullName);      
>                     item.Accept(this);
>                 }
>             }
>         }
>
> *************************************************************************** 
> ************
> ...on this project...
> *************************************************************************** 
> ************
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
> using ClassLibrary1;
>
> namespace TestTest
> {
>     class Test
>     {
>         class innerClassTest
>         {
>             int xyz;
>         }
>
>         struct structTest
>         {
>             int abc;
>         }
>
>         static void Main(string[] args)
>         {
>         }
>     }
>
> }
>
> --------------------------------------------------------------------------- 
> ----------------------------------------------------------------

-- 
--
mono-cecil

Reply via email to