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


C# ParameterInfo.IsReturnValue方法代码示例

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


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

示例1: AppendParameter

            /// <summary>
            /// Appends a parameter to the buffer.
            /// </summary>
            /// <param name="parameter">
            /// Information about the parameter to append.
            /// </param>
            /// <param name="argument">
            /// The argument that was passed to the parameter.
            /// </param>
            private void AppendParameter(ParameterInfo parameter, object argument)
            {
                bool sensitive = this.sensitiveMethod || parameter.IsDefined(typeof(SensitiveDataAttribute), false);

                try
                {
                    if (!parameter.IsReturnValue())
                    {
                        this.buffer.Append(parameter.Name);
                    }

                    this.buffer.Append('=');

                    if (argument == null)
                    {
                        this.buffer.Append("<null>");
                        return;
                    }

#if DEBUG

                    // in debug builds sensitive data may be traced with the appropriate switch; in release builds it may not
                    if (sensitive && !StyleCopTrace.Switch.TraceSensitiveData)
#else
                    if (sensitive) 
#endif
                    {
                        this.buffer.Append("<obscured>");
                        return;
                    }

                    // if the argument is a string then print it with quotes
                    if (argument is string)
                    {
                        this.buffer.Append("\"" + argument + "\"");
                        return;
                    }

                    // if the argument is a type then print it as a typeof
                    if (argument is Type)
                    {
                        this.buffer.Append("typeof(" + ((Type)argument).Name + ")");
                        return;
                    }

                    // if the argument is a primitive (or pseudo-primitive) type then print it 'as is'
                    Type argumentType = argument.GetType();
                    if (argumentType.IsPrimitive || argumentType == typeof(decimal))
                    {
                        this.buffer.Append(argument);
                        return;
                    }

                    // if it has an overridden ToString method print it in curly brackets
                    MethodInfo stringMethod = argumentType.GetMethod("ToString", BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null);
                    if (stringMethod.GetBaseDefinition().DeclaringType != stringMethod.DeclaringType)
                    {
                        this.buffer.Append("{" + argument + "}");
                        return;
                    }

                    // if the argument type has a DebuggerDisplayAttribute then format and print in square brackets
                    DebuggerDisplayAttribute displayAttribute =
                        (DebuggerDisplayAttribute)argumentType.GetCustomAttributes(typeof(DebuggerDisplayAttribute), true).FirstOrDefault();
                    if (displayAttribute != null)
                    {
                        MatchEvaluator evaluator = match =>
                            {
                                string memberName = match.Value.Replace("{", null).Replace("}", null);
                                PropertyInfo propertyInfo = argumentType.GetProperty(
                                    memberName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                                if (propertyInfo != null)
                                {
                                    return Convert.ToString(propertyInfo.GetValue(argument, null), CultureInfo.InvariantCulture);
                                }

                                FieldInfo fieldInfo;
                                Type typeToInspect = argumentType;
                                do
                                {
                                    fieldInfo = typeToInspect.GetField(memberName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                                    typeToInspect = typeToInspect.BaseType;
                                }
                                while (fieldInfo == null && typeToInspect != typeof(object));
                                return fieldInfo != null ? Convert.ToString(fieldInfo.GetValue(argument), CultureInfo.InvariantCulture) : "?";
                            };

                        string displayString = DebuggerDisplayFormatRegex.Replace(displayAttribute.Value, evaluator);
                        this.buffer.Append('[');
                        this.AppendTypeName(argumentType);
                        this.buffer.Append(": ").Append(displayString).Append(']');
//.........这里部分代码省略.........
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:101,代码来源:StyleCopTraceFormatter.cs


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