Click here to Skip to main content
15,884,629 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 Nova.Parsing;
using Nova.Rendering;

namespace Nova.CodeDOM
{
    /// <summary>
    /// Constrains the type that a <see cref="TypeParameter"/> can represent to the specified type (or a derived type).
    /// The constraining type can be a class type, an interface type, or a type parameter type.
    /// </summary>
    public class TypeConstraint : TypeParameterConstraint
    {
        #region /* FIELDS */

        /// <summary>
        /// Should evaluate to a reference to a class type, interface type, or type parameter type.
        /// </summary>
        protected Expression _type;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a <see cref="TypeConstraint"/> instance.
        /// </summary>
        /// <param name="type">An <see cref="Expression"/> that evaluates to a <see cref="TypeRef"/>.</param>
        public TypeConstraint(Expression type)
        {
            Type = type;
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The type <see cref="Expression"/>.
        /// </summary>
        public Expression Type
        {
            get { return _type; }
            set { SetField(ref _type, value, true); }
        }

        #endregion

        #region /* METHODS */

        /// <summary>
        /// Deep-clone the code object.
        /// </summary>
        public override CodeObject Clone()
        {
            TypeConstraint clone = (TypeConstraint)base.Clone();
            clone.CloneField(ref clone._type, _type);
            return clone;
        }

        #endregion

        #region /* PARSING */

        /// <summary>
        /// Parse a <see cref="TypeConstraint"/>.
        /// </summary>
        public TypeConstraint(Parser parser, CodeObject parent)
            : base(parser, parent)
        {
            SetField(ref _type, Expression.Parse(parser, this), true);
        }

        #endregion

        #region /* FORMATTING */

        /// <summary>
        /// Determines if the code object only requires a single line for display.
        /// </summary>
        public override bool IsSingleLine
        {
            get { return (base.IsSingleLine && (_type == null || (!_type.IsFirstOnLine && _type.IsSingleLine))); }
            set
            {
                base.IsSingleLine = value;
                if (value && _type != null)
                {
                    _type.IsFirstOnLine = false;
                    _type.IsSingleLine = true;
                }
            }
        }

        #endregion

        #region /* RENDERING */

        protected override void AsTextConstraint(CodeWriter writer, RenderFlags flags)
        {
            _type.AsText(writer, flags);
        }

        #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