当前位置: 首页>>代码示例>>C#>>正文


C# Method.TryGetDeclaringType方法代码示例

本文整理汇总了C#中Method.TryGetDeclaringType方法的典型用法代码示例。如果您正苦于以下问题:C# Method.TryGetDeclaringType方法的具体用法?C# Method.TryGetDeclaringType怎么用?C# Method.TryGetDeclaringType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Method的用法示例。


在下文中一共展示了Method.TryGetDeclaringType方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GeneratePUTCommand

        public static string GeneratePUTCommand(Method putmethod)
        {
            TypeEx classdef;
            bool bresult = putmethod.TryGetDeclaringType(out classdef);
            SafeDebug.Assume(bresult, "Declaring type cannot be null");

            var assemblyEx = classdef.Definition.Module.Assembly;
            var assemblyName = assemblyEx.Location;
            var namespacestr = classdef.Namespace;

            StringBuilder sb = new StringBuilder();
            sb.Append("pex.exe ");
            sb.Append(assemblyName);
            sb.Append(" /nf:");
            sb.Append(namespacestr);
            sb.Append(" /tf:");
            sb.Append(classdef.ShortName.ToString() + "!");
            sb.Append(" /mf:");
            sb.Append(putmethod.ShortName.ToString() + "!");
            sb.Append(" /nor");
            sb.Append(" /rn:");
            sb.Append(classdef.ShortName.ToString() + "_" + putmethod.ShortName.ToString());
            if(PexMeConstants.ENABLE_DEBUGGING_MODE)
                sb.Append(" /bos");
            sb.Append(" /fullmscorlib");
            return sb.ToString();
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:27,代码来源:PUTGenerator.cs

示例2: DerivedArguments

        public override void DerivedArguments(int frameId, Method method, Term[] arguments)
        {
            this.derivedArguments[frameId] = (Term[]) arguments.Clone();
            if (trackedFrameId == frameId)
            {
                TypeEx declaringType;
                if (method.TryGetDeclaringType(out declaringType) &&
                    declaringType.IsReferenceType &&
                    !method.IsStatic)
                {
                    var receiver = arguments[0];

                    var instanceFields = declaringType.InstanceFields;
                    Term[] initialFieldValues = new Term[instanceFields.Length];
                    Term[] initialFieldArrayValues = new Term[instanceFields.Length];
                    for (int i = 0; i < instanceFields.Length; i++)
                    {
                        initialFieldValues[i] = _thread.State.ReadObjectField(receiver, instanceFields[i]);
                        if (instanceFields[i].Type.Spec == TypeSpec.SzArray)
                            initialFieldArrayValues[i] = _thread.State.ReadSzArrayElement(instanceFields[i].Type.ElementType.Layout, initialFieldValues[i], this.TermManager.Symbol(SzArrayIndexId.Instance));
                    }

                    this.initialFieldValues[frameId] = initialFieldValues;
                    this.initialFieldArrayValues[frameId] = initialFieldArrayValues;
                }
            }
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:27,代码来源:FieldAccessCollector.cs

示例3: TryGetCallingMethodsInType

        /// <summary>
        /// Gets calling methods of a given called method on the given field in a given target type
        /// </summary>
        /// <returns></returns>
        public bool TryGetCallingMethodsInType(Method calledMethod, Field field, TypeEx targetType, out SafeSet<Method> callingMethods)
        {
            SafeDebug.AssumeNotNull(calledMethod, "method");
            SafeDebug.AssumeNotNull(targetType, "type");

            //Get the associated property of the field
            Property property;
            if (!MethodOrFieldAnalyzer.TryGetPropertyReadingField(this, targetType, field, out property))
            {
                //TODO: error;
            }

            MethodStore mstore = null;
            if (this.MethodDictionary.TryGetValue(calledMethod, out mstore))
            {
                if (mstore.CallingMethods.TryGetValue(targetType, out callingMethods))
                    return true;
            }

            //No method store found. create a fresh one
            if (mstore == null)
            {
                mstore = new MethodStore();
                mstore.methodName = calledMethod;
                this.MethodDictionary[calledMethod] = mstore;
            }

            callingMethods = new SafeSet<Method>();
            mstore.CallingMethods[targetType] = callingMethods;
            TypeEx calledMethodType;
            if (!calledMethod.TryGetDeclaringType(out calledMethodType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "callingmethods",
                    "Failed to get the declaring type for the method " + calledMethod.FullName);
                return false;
            }

            //Needs to addess the array type issue over here.
            var targetdef = targetType.Definition;
            if (targetdef == null)
            {
                if (targetType.TypeKind != TypeKind.SzArrayElements)
                {
                    this.Log.LogError(WikiTopics.MissingWikiTopic, "callingmethods",
                        "The definition for the type " + targetType.FullName + " is null");
                    return false;
                }
                else
                {
                    targetdef = targetType.ElementType.Definition;
                }
            }

            //Analyze each method in the given type to identify the calling methods
            //of the given method
            foreach (var typeMethod in targetdef.DeclaredInstanceMethods)
            {
                Method minstance = typeMethod.Instantiate(targetType.GenericTypeArguments, MethodOrFieldAnalyzer.GetGenericMethodParameters(this, typeMethod));

                MethodEffects meffects;
                if (!MethodOrFieldAnalyzer.TryComputeMethodEffects(this, targetType, minstance, null, out meffects))
                    continue;

                //Check for a direct comparison
                if (meffects.DirectCalledMethods.Contains(calledMethod))
                    callingMethods.Add(minstance);
                else
                {
                    //Use vtable lookup for addressing the abstract issues
                    foreach (var dcallMethod in meffects.DirectCalledMethods)
                    {
                        if (dcallMethod.IsConstructor)
                            continue;

                        TypeEx dcallMethodType;
                        if (dcallMethod.TryGetDeclaringType(out dcallMethodType))
                        {
                            if (dcallMethodType == calledMethodType || !dcallMethodType.IsAbstract || !dcallMethodType.IsInterface)
                                continue;

                            if (!dcallMethodType.IsAssignableTo(calledMethodType) && !calledMethodType.IsAssignableTo(dcallMethodType))
                                continue;
                        }

                        try
                        {
                            var lookupMethod = calledMethodType.VTableLookup(dcallMethod);
                            if (lookupMethod != null && calledMethod == lookupMethod)
                            {
                                callingMethods.Add(minstance);
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            this.Log.LogWarningFromException(ex, WikiTopics.MissingWikiTopic,
//.........这里部分代码省略.........
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:101,代码来源:PexMeStaticDatabase.cs

示例4: IsAnObserver

        /// <summary>
        /// Checks whether a given method is an observer or not
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool IsAnObserver(Method method)
        {
            TypeEx declaringType;
            if (!method.TryGetDeclaringType(out declaringType))
                return false;

            MethodEffects me;
            if (!MethodOrFieldAnalyzer.TryComputeMethodEffects(this, declaringType, method,
                null, out me))
                return false;

            if (me.WrittenInstanceFields.Count == 0)
                return true;

            return false;
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:21,代码来源:PexMeStaticDatabase.cs

示例5: AddMonitoredField

        /// <summary>
        /// Adds a monitored field to the database. Updates two kinds of hashmaps
        /// a. Field to Method mapper, which gives what the methods modifying a given field
        /// b. Method to Field mapper, which gives what fields are modified by each method (later used to identify a minimized set of methods)
        /// </summary>
        /// <param name="tm"></param>
        /// <param name="method"></param>
        /// <param name="f"></param>
        /// <param name="indices"></param>
        /// <param name="fieldValue"></param>
        public void AddMonitoredField(TermManager tm, Method method, Field f, Term[] indices, Term fieldValue, Term initialValue)
        {
            string arrayIndex = "";
            using (PexMeTermRewriter pexmeRewriter = new PexMeTermRewriter(tm))
            {
                fieldValue = pexmeRewriter.VisitTerm(default(TVoid), fieldValue); //update the field value to accomodate array-type field
                //if (indices.Length == 0) //not an array-type field
                if (indices.Length == 1) //is an array-type field
                {
                    arrayIndex = " at index of " + indices[0].UniqueIndex.ToString();
                }

                if(initialValue != null)
                    initialValue = pexmeRewriter.VisitTerm(default(TVoid), initialValue);
            }

            //Updating the method store
            MethodStore ms;
            if(!methodDic.TryGetValue(method, out ms))
            {
                ms = new MethodStore();
                ms.methodName = method;
                methodDic[method] = ms;
            }

            ms.WriteFields.Add(f);
            //TODO: Gather information of read fields

            //Updating the field store
            FieldStore fs;
            if (!fieldDic.TryGetValue(f, out fs))
            {
                fs = new FieldStore();
                fs.FieldName = f;
                fieldDic[f] = fs;
            }

            TypeEx declaringType;
            if (!method.TryGetDeclaringType(out declaringType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "monitorfield",
                    "Failed to get the declaring type for the method " + method.FullName);
                return;
            }

            SafeSet<Method> writeMethods;
            if (!fs.WriteMethods.TryGetValue(declaringType, out writeMethods))
            {
                writeMethods = new SafeSet<Method>();
                fs.WriteMethods[declaringType] = writeMethods;
            }
            writeMethods.Add(method);

            var sb = new SafeStringBuilder();
            var swriter = new TermSExpWriter(tm, new SafeStringWriter(sb), true, false);
            swriter.Write(fieldValue);
            sb.Append(arrayIndex);

            int value;
            if (tm.TryGetI4Constant(fieldValue, out value))
            {
                int initialval;
                if (initialValue != null)
                    tm.TryGetI4Constant(initialValue, out initialval);
                else
                    initialval = 0;

                sb.Append("  constant value: " + value);

                if (f.Type.ToString() != "System.Boolean")
                {
                    if (value < initialval)
                        fs.ModificationTypeDictionary[method] = FieldModificationType.DECREMENT;
                    else if (value > initialval)
                    {
                        fs.ModificationTypeDictionary[method] = FieldModificationType.INCREMENT;

                        if (value == initialval + 1)
                            fs.PreciseModificationTypeDictionary[method] = FieldModificationType.INCREMENT_ONE;
                    }
                    else
                        fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN;
                }
                else
                {
                    if (value == 0)
                        fs.ModificationTypeDictionary[method] = FieldModificationType.FALSE_SET;
                    else
                        fs.ModificationTypeDictionary[method] = FieldModificationType.TRUE_SET;
                }
//.........这里部分代码省略.........
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:101,代码来源:PexMeDynamicDatabase.cs

示例6: AddMethodMapping

        /// <summary>
        /// Makes a mapping relationship between a calling method and a called method
        /// </summary>
        /// <param name="callingMethod"></param>
        /// <param name="calledMethod"></param>
        public void AddMethodMapping(Method callingMethod, Method calledMethod)
        {
            //Updating the method store for calling methods
            MethodStore callingMethodStore = null;
            if (!methodDic.TryGetValue(callingMethod, out callingMethodStore))
            {
                callingMethodStore = new MethodStore();
                callingMethodStore.methodName = calledMethod;
                methodDic[callingMethod] = callingMethodStore;
            }
            callingMethodStore.CalledMethods.Add(calledMethod);

            //Updating the method store for called methods
            MethodStore calledMethodStore = null;
            if(!methodDic.TryGetValue(calledMethod, out calledMethodStore))
            {
                calledMethodStore = new MethodStore();
                calledMethodStore.methodName = calledMethod;
                methodDic[calledMethod] = calledMethodStore;
            }

            TypeEx callingMethodType;
            if (!callingMethod.TryGetDeclaringType(out callingMethodType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "methodmapping",
                    "Failed to get the declared type for method " + calledMethod);
                return;
            }

            SafeSet<Method> localCallingMethods;
            if (!calledMethodStore.CallingMethods.TryGetValue(callingMethodType, out localCallingMethods))
            {
                localCallingMethods = new SafeSet<Method>();
                calledMethodStore.CallingMethods[callingMethodType] = localCallingMethods;
            }
            localCallingMethods.Add(callingMethod);
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:42,代码来源:PexMeDynamicDatabase.cs

示例7: TryRetrievePUT

        /// <summary>
        /// Retrieves the PUT for the method we are looking for in the current assembly
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static bool TryRetrievePUT(IPexComponent host, AssemblyEx assembly, Method method, out Method putmethod)
        {
            //Get the class name of the target class that is including
            //the method we are looking for
            TypeEx declaringType;
            bool bresult = method.TryGetDeclaringType(out declaringType);
            SafeDebug.Assume(bresult, "Failed to get the declaring type!!!");

            string tclassname = null;
            var declaringTypeDef = declaringType.Definition;
            if (declaringTypeDef.GenericTypeParametersCount == 0)
            {
                tclassname = declaringType.ShortName.ToString();
            }
            else
            {
                tclassname = declaringType.ShortName.ToString();
                tclassname = tclassname.Substring(0, tclassname.IndexOf('`'));

                foreach (var param in declaringTypeDef.GenericTypeParameters)
                {
                    tclassname = tclassname + param.Name;
                }
            }
            tclassname = tclassname + "Test";

            foreach (var classdef in assembly.TypeDefinitions)
            {
                if(classdef.ShortName != tclassname)
                    continue;

                //TODO: To be more robust, we can later replace
                //this piece of code by actually checking whether there
                //is a method call to the method of the library we are looking for
                foreach (var mdef in classdef.DeclaredInstanceMethods)
                {
                    if (mdef.ShortName == method.ShortName)
                    {
                        bool isPexMethod = false;
                        foreach (var attr in mdef.DeclaredAttributes)
                        {
                            if (attr.SerializableName.ToString().Contains("PexMethodAttribute"))
                            {
                                isPexMethod = true;
                                break;
                            }
                        }

                        if (isPexMethod)
                        {
                            putmethod = mdef.Instantiate(MethodOrFieldAnalyzer.GetGenericTypeParameters(host, classdef),
                                MethodOrFieldAnalyzer.GetGenericMethodParameters(host, mdef));
                            return true;
                        }
                    }
                }
            }

            putmethod = null;
            return false;
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:66,代码来源:PUTGenerator.cs

示例8: PropagateModificationsToCaller

        /// <summary>
        /// Propagates the changes made by a called method to its caller, based on the lastLoadedFields        
        /// For example, if the called method modifies any fields, then the related parent fields in the caller method
        /// are marked as updated
        /// </summary>
        /// <param name="callerMStore"></param>
        /// <param name="calledMethodStore"></param>
        private void PropagateModificationsToCaller(SEMethodStore callerMStore, SEMethodStore calledMethodStore, Method calledMethod)
        {
            if (this.lastLoadedFields.Count == 0)
                return;

            //Idenitfy relevant fields from the last loadedFields
            SafeList<Field> fieldsProcessed = new SafeList<Field>();
            Field receiverField = null;

            //Check whether the current method call is actually on the field previously loaded
            TypeEx methodDeclType;
            if (!calledMethod.TryGetDeclaringType(out methodDeclType))
            {
                logger.Error("Failed to get the declaring type of the method: " + calledMethod.FullName);
                return;
            }

            if (methodDeclType.FullName == "System.String" || methodDeclType.FullName == "System.string")
            {
                return; //Do not process string types.
            }

            //Check whether the declaring type is in the fields list
            foreach (var field in this.lastLoadedFields)
            {
                var fieldType = field.Type;
                if (fieldType == methodDeclType || fieldType.IsAssignableTo(methodDeclType) || methodDeclType.IsAssignableTo(fieldType))
                {
                    fieldsProcessed.Add(field);
                    receiverField = field;
                    break;
                }
            }

            if (receiverField == null)
            {
                //Failed to identify the reciver field of this method
                return;
            }

            //Identify arguments of the current method call
            foreach (var argtype in calledMethod.ParameterTypes)
            {
                foreach (var field in this.lastLoadedFields)
                {
                    if (field == receiverField)
                        continue;

                    var fieldType = field.Type;
                    if (fieldType == argtype || fieldType.IsAssignableTo(argtype) || argtype.IsAssignableTo(fieldType))
                    {
                        fieldsProcessed.Add(field);
                        break;
                    }
                }
            }

            if (calledMethodStore.DefinedFieldSet.Count == 0)
            {
                //If the called method does not define any field, all current fields are just loaded fields
                foreach (var field in fieldsProcessed)
                {
                    callerMStore.AddToUsedList(field, this.currOffset);
                    this.lastLoadedFields.Remove(field);
                }
            }
            else
            {
                //process each defined field
                foreach (var deffield in calledMethodStore.DefinedFieldSet.Values)
                {
                    TypeEx deffieldType;
                    if (deffield.OptionalField.TryGetDeclaringType(out deffieldType))
                    {
                        //Identify the related field in the lastLoadedFields
                        Field processedField = null;
                        foreach (var field in fieldsProcessed)
                        {
                            var fieldType = field.Type;
                            if (fieldType == deffieldType || fieldType.IsAssignableTo(deffieldType) || deffieldType.IsAssignableTo(fieldType))
                            {
                                processedField = field;
                                callerMStore.AddToDefinedList(field, this.currOffset);
                                break;
                            }
                        }

                        if (processedField != null)
                        {
                            fieldsProcessed.Remove(processedField);
                            this.lastLoadedFields.Remove(processedField);
                        }
                    }
//.........这里部分代码省略.........
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:101,代码来源:SETracer.cs


注:本文中的Method.TryGetDeclaringType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。