Click here to Skip to main content
15,884,836 members
Articles / Programming Languages / C#

Accessing Assembly Metadata with Reflection or Mono Cecil (Part 6)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
2 Dec 2012CDDL16 min read 42.2K   936   20  
Loading type metadata from assemblies with Reflection or Mono Cecil.
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using Mono.Cecil;

namespace Nova.Utilities
{
    /// <summary>
    /// Static helper methods for <see cref="ParameterDefinition"/>.
    /// </summary>
    public static class ParameterDefinitionUtil
    {
        #region /* STATIC HELPER METHODS */

        /// <summary>
        /// The name of the 'params' attribute.
        /// </summary>
        public const string ParamArrayAttributeName = "ParamArrayAttribute";

        /// <summary>
        /// Check if the ParameterDefinition has the 'params' modifier.
        /// </summary>
        public static bool IsParams(ParameterDefinition parameterDefinition)
        {
            return ICustomAttributeProviderUtil.HasCustomAttribute(parameterDefinition, ParamArrayAttributeName);
        }

        /// <summary>
        /// Check if the ParameterDefinition has the 'ref' modifier.
        /// </summary>
        public static bool IsRef(ParameterDefinition parameterDefinition)
        {
            // A 'ref' will generally NOT have the "[In]" and "[Out]" attributes, so assume 'ref' for an IsByRef type where
            // the parameter is NOT an 'out'.
            return (parameterDefinition.ParameterType.IsByReference && !(parameterDefinition.IsOut && !parameterDefinition.IsIn));
        }

        /// <summary>
        /// Check if the ParameterDefinition has the 'out' modifier.
        /// </summary>
        public static bool IsOut(ParameterDefinition parameterDefinition)
        {
            // The IsIn and IsOut properties reflect the "[In]" and "[Out]" attributes, NOT the C# 'ref' and 'out' keywords.
            // An 'out' parameter will always have an IsByRef type, and should always have the "[Out]" attribute to distinguish
            // it from a 'ref'.  To be thorough, also ensure that there isn't an "[In]" attribute.
            return (parameterDefinition.ParameterType.IsByReference && parameterDefinition.IsOut && !parameterDefinition.IsIn);
        }

        /// <summary>
        /// Get the category name.
        /// </summary>
        public static string GetCategory(ParameterDefinition parameterDefinition)
        {
            return "parameter";
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer (Senior)
United States United States
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).

Comments and Discussions