Click here to Skip to main content
15,886,014 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 System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.DelegateDecl"/>.
    /// </summary>
    public class DelegateDeclVM : TypeDeclVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(DelegateDecl),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new DelegateDeclVM((DelegateDecl)codeObject, isDescription, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ExpressionVM _returnTypeVM;
        protected ChildListVM<ParameterDeclVM> _parameterVMs;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.DelegateDecl"/>.
        /// </summary>
        public DelegateDeclVM(DelegateDecl delegateDecl, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(delegateDecl, isDescription, dictionary)
        {
            _returnTypeVM = (ExpressionVM)CreateVM(delegateDecl.ReturnType, isDescription, dictionary);
            _parameterVMs = CreateListVM<ParameterDecl, ParameterDeclVM>(delegateDecl.Parameters, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.DelegateDecl"/> model.
        /// </summary>
        public DelegateDecl DelegateDecl
        {
            get { return (DelegateDecl)CodeObject; }
        }

        /// <summary>
        /// The return type of the delegate (never null - will be type 'void' instead).
        /// </summary>
        public ExpressionVM ReturnTypeVM
        {
            get { return (_returnTypeVM ?? (TypeRefVM)CreateVM(TypeRef.VoidRef)); }
        }

        /// <summary>
        /// The list of <see cref="ParameterDeclVM"/>s.
        /// </summary>
        public ChildListVM<ParameterDeclVM> ParameterVMs
        {
            get { return _parameterVMs; }
        }

        /// <summary>
        /// True if there are any parameters.
        /// </summary>
        public bool HasParameterVMs
        {
            get { return (_parameterVMs != null && _parameterVMs.Count > 0); }
        }

        /// <summary>
        /// The number of parameters.
        /// </summary>
        public int ParameterVMCount
        {
            get { return (_parameterVMs != null ? _parameterVMs.Count : 0); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public static readonly Brush StaticBorderBrush = DarkLavender;
        public static readonly Brush StaticBackgroundBrush = Brushes.Lavender;

        public override Brush BorderBrush
        {
            get { return StaticBorderBrush; }
        }

        public override Brush BackgroundBrush
        {
            get { return StaticBackgroundBrush; }
        }

        protected override bool RenderParens()
        {
            return !MethodDeclBaseVM.HideMethodParens;
        }

        protected override void RenderStatement(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            renderer.RenderText(DelegateDecl.ParseToken, KEYWORD_BRUSH, this);
            _returnTypeVM.Render(renderer, passFlags | RenderFlags.IsPrefix | RenderFlags.PrefixSpace);
            base.RenderArgument(renderer, flags);
        }

        protected override void RenderArgumentPrefix(CodeRenderer renderer, RenderFlags flags)
        {
            if (MethodDeclBaseVM.HideMethodParens && ((_parameterVMs != null && _parameterVMs.Count > 0) || DelegateDecl.HasInfixComments))
                base.RenderArgumentPrefix(renderer, flags);
        }

        protected override void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            RenderInfixComments(renderer, 0, flags);
            renderer.RenderList(_parameterVMs, passFlags, this);
            if (DelegateDecl.IsEndFirstOnLine)
                renderer.NewLine();
        }

        public override void UnRender()
        {
            _returnTypeVM.UnRender();
            ChildListHelpers.UnRender(_parameterVMs);
            base.UnRender();
        }

        /// <summary>
        /// Get the <see cref="FrameworkElement"/> to be highlighted when the object is selected.
        /// </summary>
        public override FrameworkElement GetSelectionElement()
        {
            return FrameworkElement;
        }

        #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