public static CorFunction GetFuntion(string Module, string className, string funtionName)
        {
            CorModule module = null;
            CorFunction fun = null;
            DebugCache.LoadedModules.TryGetValue(Module, out module);
            if (module == null) {
                throw new BreakPointException("Module not loaded " + Module);
            }
            int token = 0;
            try
            {
                module.Importer.FindTypeDefByName(className, 0, out token);
            }
            catch (Exception) {
                throw new BreakPointException(className + " class is not found in" + Module);
            }
            MetaType type = new MetaType(module.Importer, token);
            try
            {
                MetadataMethodInfo method = type.GetMethod(funtionName);
                fun = module.GetCorFuntion((uint)method.MetadataToken);
            }
            catch (Exception) {
                throw new BreakPointException(funtionName + " Method is not found in" + className);
            }

            return fun;
            //method.MetadataToken
        }
 internal void SetBreakPoint(CorModule module, string className, string methodName, INotification _lisenter)
 {
     BreakPointInfo breakpoint = new BreakPointInfo(module.Name, className, methodName, null, _lisenter);
     if (!breakStringVsBP.ContainsKey(breakpoint.Identifier)){
         int token = 0;
         CorFunction fun = null;
         try{
             module.Importer.FindTypeDefByName(className, 0, out token);
         } catch (Exception){
             throw new BreakPointException(className + " class is not found in" + module.Name);
         }
         MetaType type = new MetaType(module.Importer, token);
         try{
             List<BreakPointInfo> bps = new List<BreakPointInfo>();
             foreach (MetadataMethodInfo methodInfo in type.GetMethods(methodName)){
                 BreakPointInfo bp = new BreakPointInfo(module.Name, className, methodName, null, _lisenter);
                 fun = module.GetCorFuntion((uint)methodInfo.MetadataToken);
                 bp.bpoint = fun.CreateBreakPoint();
                 bp.bpoint.Activate();
                 bps.Add(bp);
             }
             if(bps.Count > 0){
                 breakStringVsBP.Add(bps[0].Identifier, bps);
             }
         } catch (Exception) {
             throw new BreakPointException(methodName + " Method is not found in" + className);
         }
     }
 }
        internal MetadataFieldInfo(IMetadataImport importer,int fieldToken, MetaType declaringType)
        {
            m_importer = importer;
            m_fieldToken = fieldToken;
            m_declaringType = declaringType;

            // Initialize
            int mdTypeDef;
            int pchField,pcbSigBlob,pdwCPlusTypeFlab,pcchValue, pdwAttr;
            IntPtr ppvSigBlob;
            IntPtr ppvRawValue;
            m_importer.GetFieldProps(m_fieldToken,
                                     out mdTypeDef,
                                     null,
                                     0,
                                     out pchField,
                                     out pdwAttr,
                                     out ppvSigBlob,
                                     out pcbSigBlob,
                                     out pdwCPlusTypeFlab,
                                     out ppvRawValue,
                                     out pcchValue
                                     );

            StringBuilder szField = new StringBuilder(pchField);
            m_importer.GetFieldProps(m_fieldToken,
                                     out mdTypeDef,
                                     szField,
                                     szField.Capacity,
                                     out pchField,
                                     out pdwAttr,
                                     out ppvSigBlob,
                                     out pcbSigBlob,
                                     out pdwCPlusTypeFlab,
                                     out ppvRawValue,
                                     out pcchValue
                                     );
            m_fieldAttributes = (FieldAttributes)pdwAttr;
            m_name = szField.ToString();

            // Get the values for static literal fields with primitive types
            FieldAttributes staticLiteralField = FieldAttributes.Static | FieldAttributes.HasDefault | FieldAttributes.Literal;
            if ((m_fieldAttributes & staticLiteralField) == staticLiteralField)
            {
                m_value = ParseDefaultValue(declaringType,ppvSigBlob,ppvRawValue);
            }
        }
        internal void PostBreakPoint(CorThread thread)
        {
            string moduleName = thread.ActiveFrame.Function.Module.Name;
            MetaType type = new MetaType(
                thread.ActiveFrame.Function.Module.Importer, (int)thread.ActiveFrame.Function.Class.Token);
            string className = type.Name;
            string methodName = type.FindMethod((int)thread.ActiveFrame.Function.Token).Name;

            methodName = methodName.Substring(methodName.LastIndexOf('.')+1);
            string identifier = BreakPointManager.getUID(moduleName, className, methodName);
            INotification notification = m_bpManger.GetNotificaiton(identifier);
            if (notification != null) {
                InnerObject innerObject
                    = new InnerObject(
                        notification,
                        new BreakPointNotificationResult(
                            m_ProcessID,
                            thread.ID,
                            moduleName,
                            className,
                            methodName));
                ThreadPool.QueueUserWorkItem(new WaitCallback(SendNotifications), innerObject);
            }
            //List<BreakPointInfo> m_bpManger.GetBreakPointInfo(identifier);
        }
        // returns "" for normal classes, returns prefix for nested classes
        private string GetNestedClassPrefix( TypeAttributes attribs)
        {
            if ((attribs & TypeAttributes.VisibilityMask) > TypeAttributes.Public)
            {
                // it is a nested class
                int enclosingClass;
                m_importer.GetNestedClassProps(m_token, out enclosingClass);
                MetaType mt = new MetaType(m_importer, enclosingClass);

                return mt.Name + "+";
            }
            else
                return String.Empty;
        }
        private MDbgValue[] InternalGetFields()
        {
            List<MDbgValue> al = new List<MDbgValue>();

            //dereference && (unbox);
            CorValue value = Dereference(CorValue, null);
            if (value == null)
            {
                throw new Exception("null value");
            }
            Unbox(ref value);
            CorObjectValue ov = value.CastToObjectValue();

            CorType cType = ov.ExactType;

            CorFunctionFrame cFrame = null;

            // initialization
            CorClass corClass = ov.Class;

            // iteration through class hierarchy
            while (true)
            {
                Type classType = new MetaType(new CorModule(corClass.m_module).Importer, (int)corClass.Token);

                foreach (MetadataFieldInfo fi in classType.GetFields()){
                    CorValue fieldValue = null;
                    try{
                        if (fi.IsLiteral){
                            fieldValue = null;
                            // for now we just hide the constant fields.
                            continue;
                        }else if (fi.IsStatic){
                            if (cFrame == null){
                                // Without a frame, we won't be able to find static values.  So
                                // just skip this guy
                                continue;
                            }

                            fieldValue = cType.GetStaticFieldValue(fi.MetadataToken, cFrame);
                        }
                        else
                        {
                            // we are asuming normal field value
                            fieldValue = ov.GetFieldValue(corClass, fi.MetadataToken);
                        }
                    }
                    catch (COMException)
                    {
                        // we won't report any problems.
                    }
                    al.Add(new MDbgValue( fi.Name, fieldValue));
                }
                cType = cType.Base;
                if (cType == null)
                    break;
                corClass = cType.Class;
                //classModule = Process.Modules.Lookup(corClass.Module);
            }

            return al.ToArray();
        }
        // Print CorType to the given string builder.
        // Will print generic info.
        internal static void PrintCorType(StringBuilder sb, CorType ct)
        {
            switch (ct.Type)
            {
                case CorElementType.ELEMENT_TYPE_CLASS:
                case CorElementType.ELEMENT_TYPE_VALUETYPE:
                    // We need to get the name from the metadata. We can get a cached metadata importer
                    // from a MDbgModule, or we could get a new one from the CorModule directly.
                    // Is this hash lookup to get a MDbgModule cheaper than just re-querying for the importer?
                    CorClass cc = ct.Class;
                    MetaType retType = new MetaType(new CorModule(cc.m_module).Importer, (int)cc.Token);
                    string typeName = retType.Name;
                    if (!Regex.IsMatch(typeName, @"^[a-zA-Z0-9_.]+$")) {
                        typeName = "**UNK**";
                    }
                    sb.Append(typeName);
                    return;

                // Primitives
                case CorElementType.ELEMENT_TYPE_BOOLEAN:
                    sb.Append("System.Boolean"); return;
                case CorElementType.ELEMENT_TYPE_CHAR:
                    sb.Append("System.Char"); return;
                case CorElementType.ELEMENT_TYPE_I1:
                    sb.Append("System.SByte"); return;
                case CorElementType.ELEMENT_TYPE_U1:
                    sb.Append("System.Byte"); return;
                case CorElementType.ELEMENT_TYPE_I2:
                    sb.Append("System.Int16"); return;
                case CorElementType.ELEMENT_TYPE_U2:
                    sb.Append("System.UInt16"); return;
                case CorElementType.ELEMENT_TYPE_I4:
                    sb.Append("System.Int32"); return;
                case CorElementType.ELEMENT_TYPE_U4:
                    sb.Append("System.Uint32"); return;
                case CorElementType.ELEMENT_TYPE_I8:
                    sb.Append("System.Int64"); return;
                case CorElementType.ELEMENT_TYPE_U8:
                    sb.Append("System.UInt64"); return;
                case CorElementType.ELEMENT_TYPE_I:
                    sb.Append("System.IntPtr"); return;
                case CorElementType.ELEMENT_TYPE_U:
                    sb.Append("System.UIntPtr"); return;
                case CorElementType.ELEMENT_TYPE_R4:
                    sb.Append("System.Single"); return;
                case CorElementType.ELEMENT_TYPE_R8:
                    sb.Append("System.Double"); return;

                // Well known class-types.
                case CorElementType.ELEMENT_TYPE_OBJECT:
                    sb.Append("System.Object"); return;
                case CorElementType.ELEMENT_TYPE_STRING:
                    sb.Append("System.String"); return;

                // Special compound types. Based off first type-param
                case CorElementType.ELEMENT_TYPE_SZARRAY:
                case CorElementType.ELEMENT_TYPE_ARRAY:
                case CorElementType.ELEMENT_TYPE_BYREF:
                case CorElementType.ELEMENT_TYPE_PTR:
                    CorType t = ct.FirstTypeParameter;
                    PrintCorType(sb,  t);
                    switch (ct.Type)
                    {
                        case CorElementType.ELEMENT_TYPE_SZARRAY:
                            sb.Append("[]");
                            return;
                        case CorElementType.ELEMENT_TYPE_ARRAY:
                            int rank = ct.Rank;
                            sb.Append('[');
                            for (int i = 0; i < rank - 1; i++)
                            {

                                sb.Append(',');
                            }
                            sb.Append(']');
                            return;
                        case CorElementType.ELEMENT_TYPE_BYREF:
                            sb.Append("&");
                            return;
                        case CorElementType.ELEMENT_TYPE_PTR:
                            sb.Append("*");
                            return;
                    }

                    return;

                case CorElementType.ELEMENT_TYPE_FNPTR:
                    sb.Append("*(...)");
                    return;
                case CorElementType.ELEMENT_TYPE_TYPEDBYREF:
                    sb.Append("typedbyref");
                    return;
                default:
                    sb.Append("<unknown>");
                    return;
            }
        }
        private static object ParseDefaultValue(MetaType declaringType, IntPtr ppvSigBlob, IntPtr ppvRawValue)
        {
            IntPtr ppvSigTemp = ppvSigBlob;
                CorCallingConvention callingConv = MetadataHelperFunctions.CorSigUncompressCallingConv(ref ppvSigTemp);
                Debug.Assert(callingConv == CorCallingConvention.Field);

                CorElementType elementType = MetadataHelperFunctions.CorSigUncompressElementType(ref ppvSigTemp);
                if (elementType == CorElementType.ELEMENT_TYPE_VALUETYPE)
                {
                        uint token = MetadataHelperFunctions.CorSigUncompressToken(ref ppvSigTemp);

                        if (token == declaringType.Token)
                        {
                            // Static literal field of the same type as the enclosing type
                            // may be one of the value fields of an enum
                            //if (declaringType.IsEnum)
                            //{
                            //    // If so, the value will be of the enum's underlying type,
                            //    // so we change it from VALUETYPE to be that type so that
                            //    // the following code will get the value
                            //    elementType = declaringType.;
                            //}
                        }
                }

                switch (elementType)
                {
                    case CorElementType.ELEMENT_TYPE_CHAR:
                        return (char)Marshal.ReadByte(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_I1:
                        return (sbyte)Marshal.ReadByte(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_U1:
                        return Marshal.ReadByte(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_I2:
                        return Marshal.ReadInt16(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_U2:
                        return (ushort)Marshal.ReadInt16(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_I4:
                        return Marshal.ReadInt32(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_U4:
                        return (uint)Marshal.ReadInt32(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_I8:
                        return Marshal.ReadInt64(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_U8:
                        return (ulong)Marshal.ReadInt64(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_I:
                        return Marshal.ReadIntPtr(ppvRawValue);
                    case CorElementType.ELEMENT_TYPE_U:
                    case CorElementType.ELEMENT_TYPE_R4:
                    case CorElementType.ELEMENT_TYPE_R8:
                    // Technically U and the floating-point ones are options in the CLI, but not in the CLS or C#, so these are NYI
                    default:
                        return null;
                }
        }
        public static CorFunction GetFuntion(CorModule module, string className, string funtionName)
        {
            CorFunction fun = null;
            int token = 0;
            try
            {
                module.Importer.FindTypeDefByName(className, 0, out token);
            }
            catch (Exception)
            {
                throw new BreakPointException(className + " class is not found in" + module.Name);
            }
            MetaType type = new MetaType(module.Importer, token);
            try
            {
                MetadataMethodInfo method = type.GetMethod(funtionName);
                fun = module.GetCorFuntion((uint)method.MetadataToken);
            }
            catch (Exception)
            {
                throw new BreakPointException(funtionName + " Method is not found in" + className);
            }

            return fun;
            //method.MetadataToken
        }