How to get System.Type from ITypeDefinition?

Topics: Metadata Model, Source Model
Apr 2, 2010 at 9:49 AM

Dear All,

I need to use CCI for a parser project. I parse code and base on if/else switches generate a mapping file.

Example:

var list = new ArrayList(); 
            if(cfg.BoolProp == true)
            {
                list.Add(Dirs.SomeDir);
            }

Mapping: BoolProp maps to SomeDir.

I encountered a problem when I try to do the same with enums:

 

var list = new ArrayList(); 
            if(cfg.EnumProp == SomeEnum.First)
            {
                list.Add(Dirs.SomeDir);
            }
I can't figure out how to get SomeEnum.First value at runtime.
I can figure out what is ITypeDefinition of property and what corresponding Int32 constant is the right operand.
But to figure the value of constant I need to know System.Type of property not ITypeDefition because I need to use it in Enum.GetName which expects System.Type.
Is there is any way to get runtime type of underlying type from ITypeDefinition?
The code to try out:
 public SomeEnum EnumProp
        {
            get
            {
                return SomeEnum.First;
            }
        }
 public void SimpleConditionWithEnum()
        {
            var list = new ArrayList(); 
            if(cfg.EnumProp == SomeEnum.First)
            {
                list.Add(Dirs.SomeDir);
            }
        }
if(equality.LeftOperand.Type.IsEnum)
            {
                var methodCall = equality.LeftOperand as IMethodCall;
                var property = ParseProperty(methodCall);
                var compileTimeConstant = equality.RightOperand as ICompileTimeConstant;
                var value = Convert.ToInt32(compileTimeConstant.Value);
//TODO:  Get  value from enum somehow
}

Thanks in advance for answering and taking the time to read the post,

Yours sincerely,
Maxim Filimonov

Coordinator
Apr 4, 2010 at 7:36 PM

There isn't currently a convenient helper function for obtaining a System.Type definition from an ITypeDefinition. It is also not clear that it is very good idea to use both metadata models in the same application.

In your particular case, you seem to be wanting to get hold of the value of an enumeration member, such as SomeEnum.First. To do that you simply have to look up the "First" field of type "SomeEnum". Its CompileTimeValue property will contain the value you are looking for. Look at TypeHelper for convenience methods. For example, TypeHelper.GetField(ITypeDefinition declaringType, IName fieldName).