/// <summary>
        /// Checks parameters for problems
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public override ProblemCollection Check(Parameter parameter)
        {
#if DEBUG
            lock (_debugLock)
            {
#endif
            UniqueProblemCollection problems = new UniqueProblemCollection();

            string currentTypeNamespace = parameter.DeclaringMethod.DeclaringType.FullName;
            string baseNamespace        = GetNamespaceFromTypeName(currentTypeNamespace, BaseNamespaceMoniker);

            if (parameter.DeclaringMethod.Body.SourceContext.IsValid)
            {
                if (!string.IsNullOrEmpty(baseNamespace))
                {
                    CallTestObjectValue(problems,
                                        null,
                                        baseNamespace,
                                        parameter.Type,
                                        string.Format("Parameter {0} declared in a method in namespace {{0}} may not reference namespace {{1}}", parameter.Name));
                }
            }

            return(problems.Problems);

#if DEBUG
        }
#endif
        }
        /// <summary>
        /// Checks types for problems
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override ProblemCollection Check(TypeNode type)
        {
#if DEBUG
            lock (_debugLock)
            {
#endif
            UniqueProblemCollection problems = new UniqueProblemCollection();

            string currentTypeNamespace = type.FullName;
            string baseNamespace        = GetNamespaceFromTypeName(currentTypeNamespace, BaseNamespaceMoniker);

            if (!string.IsNullOrEmpty(baseNamespace))
            {
                string typeName;

                //Determine the name of the type
                switch (type.NodeType)
                {
                case NodeType.DelegateNode:
                    typeName = "Delegate";
                    break;

                case NodeType.Interface:
                    typeName = "Interface";
                    break;

                default:
                    typeName = "Class";
                    break;
                }

                if (type.BaseType != null)
                {
                    CallTestObjectValue(problems, null, baseNamespace, type.BaseType, typeName + " in namespace {0} may not be based on a class from namespace {1}");
                }

                //Check implemented interfaces
                foreach (InterfaceNode interfaceType in type.Interfaces)
                {
                    CallTestObjectValue(problems, null, baseNamespace, interfaceType.FullName, typeName + " in namespace {0} may not implement an interface from namespace {1}");
                }

                if (type is DelegateNode)
                {
                    DelegateNode delegateNode = type as DelegateNode;

                    CallTestObjectValue(problems, null, baseNamespace, delegateNode.ReturnType, "Delegate in namespace {0} may not have a return type from namespace {1}");

                    foreach (Parameter parameter in delegateNode.Parameters)
                    {
                        TestObjectValue(problems, null, baseNamespace, parameter.Type, "Delegate parameter in namespace {0} may not reference namespace {1}");
                    }
                }
            }
            return(problems.Problems);

#if DEBUG
        }
#endif
        }
示例#3
0
        internal override void TestObjectValue(UniqueProblemCollection problems, SourceContext?sourceContext, string baseNamespace, object value, string messageFormat)
        {
            if (sourceContext.HasValue && !sourceContext.Value.IsValid)
            {
                return;
            }

            //Determine the type of value
            TestObjectNamespaces(value, (instructionTypeName) =>
            {
                string instructionBaseNamespace = GetNamespaceFromTypeName(instructionTypeName, ParentNamespaceMoniker);

                if (instructionBaseNamespace != null)
                {
                    Resolution res = new Resolution("Using invalid namespace", messageFormat, baseNamespace, instructionBaseNamespace);

                    if (sourceContext.HasValue)
                    {
                        problems.Add(new Problem(res, sourceContext.Value));
                    }
                    else
                    {
                        problems.Add(new Problem(res));
                    }
                }
            });
        }
        /// <summary>
        /// Parses member functions looking for bad namespaces
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public override ProblemCollection Check(Member member)
        {
#if DEBUG
            lock (_debugLock)
            {
#endif
            UniqueProblemCollection problems = new UniqueProblemCollection();

            //Check to see if instructions in a Method are illeagal
            if (member is Method)
            {
                Method method = member as Method;

                Check(problems, method);
            }
            else if (member is Field)
            {
                Field field = member as Field;

                Check(problems, field);
            }
            else if (member is PropertyNode)
            {
                PropertyNode property = member as PropertyNode;

                Check(problems, property);
            }
            else if (member is EventNode)
            {
                EventNode eventNode = member as EventNode;

                string currentTypeNamespace = eventNode.DeclaringType.FullName;
                string baseNamespace        = GetNamespaceFromTypeName(currentTypeNamespace, BaseNamespaceMoniker);

                if (!string.IsNullOrEmpty(baseNamespace))
                {
                    CallTestObjectValue(problems,
                                        null,
                                        baseNamespace,
                                        eventNode.HandlerType,
                                        string.Format("Event {0} in namespace {{0}} may not reference namespace {{1}}", eventNode.Name));
                }
            }
            else
            {
                string type = member.DeclaringType.FullName;
            }

            return(problems.Problems);

#if DEBUG
        }
#endif
        }
        /// <summary>
        /// Checks properties for errors
        /// </summary>
        /// <param name="problems"></param>
        /// <param name="property"></param>
        private void Check(UniqueProblemCollection problems, PropertyNode property)
        {
            string currentTypeNamespace = property.DeclaringType.FullName;
            string baseNamespace        = GetNamespaceFromTypeName(currentTypeNamespace, BaseNamespaceMoniker);

            if (!string.IsNullOrEmpty(baseNamespace))
            {
                CallTestObjectValue(problems,
                                    null,
                                    baseNamespace,
                                    property.Type,
                                    string.Format("Property {0} in namespace {{0}} may not reference namespace {{1}}", property.Name));
            }
        }
        /// <summary>
        /// Checks fields for errors
        /// </summary>
        /// <param name="problems"></param>
        /// <param name="field"></param>
        private void Check(UniqueProblemCollection problems, Field field)
        {
            string currentTypeNamespace = field.DeclaringType.FullName;
            string baseNamespace        = GetNamespaceFromTypeName(currentTypeNamespace, BaseNamespaceMoniker);

            if (field.Type.NodeType != NodeType.DelegateNode)
            {
                if (!string.IsNullOrEmpty(baseNamespace))
                {
                    CallTestObjectValue(problems,
                                        null,
                                        baseNamespace,
                                        field.Type,
                                        string.Format("Field {0} in namespace {{0}} may not reference namespace {{1}}", field.Name));
                }
            }
        }
        /// <summary>
        /// Performs chaks on methods
        /// </summary>
        /// <param name="problems"></param>
        /// <param name="method"></param>
        private void Check(UniqueProblemCollection problems, Method method)
        {
            string currentTypeNamespace = method.DeclaringType.FullName;
            string baseNamespace        = GetNamespaceFromTypeName(currentTypeNamespace, BaseNamespaceMoniker);

            if (!string.IsNullOrEmpty(baseNamespace))
            {
                //Validate the return type
                CallTestObjectValue(problems,
                                    null,
                                    baseNamespace,
                                    method.ReturnType,
                                    "Return value in namespace {0} may not reference namespace {1}");

                foreach (Instruction instruction in GetValidInstructions(method.Instructions))
                {
                    //Some values are collections of values. We need to check each
                    if (instruction.Value is ICollection)
                    {
                        foreach (object instructionValue in (instruction.Value as ICollection))
                        {
                            if (instructionValue != null)
                            {
                                CallTestObjectValue(problems,
                                                    instruction.SourceContext,
                                                    baseNamespace,
                                                    instructionValue,
                                                    "Method in namespace {0} may not reference namespace {1}");
                            }
                        }
                    }
                    else
                    {
                        if (instruction.Value != null)
                        {
                            CallTestObjectValue(problems,
                                                instruction.SourceContext,
                                                baseNamespace,
                                                instruction.Value,
                                                "Method in namespace {0} may not reference namespace {1}");
                        }
                    }
                }
            }
        }
        private void CallTestObjectValue(UniqueProblemCollection problems, SourceContext?sourceContext, string baseNamespace, object type, string messageFormat)
        {
            try
            {
                TestObjectValue(problems, sourceContext, baseNamespace, type, messageFormat);
            }
            catch (Exception ex)
            {
                Resolution res = new Resolution("Exception checking code {0}\n{1}", ex.Message, ex.StackTrace);

                if (sourceContext.HasValue)
                {
                    problems.Add(new Problem(res, sourceContext.Value));
                }
                else
                {
                    problems.Add(new Problem(res));
                }
            }
        }
 internal abstract void TestObjectValue(UniqueProblemCollection problems, SourceContext?sourceContext, string baseNamespace, object value, string messageFormat);