Reflector.cs :  » Testing » xUnit.net » Xunit » Sdk » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Testing » xUnit.net 
xUnit.net » Xunit » Sdk » Reflector.cs
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Xunit.Sdk{
    /// <summary>
    /// Wrapper to implement <see cref="IMethodInfo"/> and <see cref="ITypeInfo"/> using reflection.
    /// </summary>
    public static class Reflector
    {
        /// <summary>
        /// Converts an <see cref="Attribute"/> into an <see cref="IAttributeInfo"/> using reflection.
        /// </summary>
        /// <param name="attribute"></param>
        /// <returns></returns>
        public static IAttributeInfo Wrap(Attribute attribute)
        {
            return new ReflectionAttributeInfo(attribute);
        }

        /// <summary>
        /// Converts a <see cref="MethodInfo"/> into an <see cref="IMethodInfo"/> using reflection.
        /// </summary>
        /// <param name="method">The method to wrap</param>
        /// <returns>The wrapper</returns>
        public static IMethodInfo Wrap(MethodInfo method)
        {
            return new ReflectionMethodInfo(method);
        }

        /// <summary>
        /// Converts a <see cref="Type"/> into an <see cref="ITypeInfo"/> using reflection.
        /// </summary>
        /// <param name="type">The type to wrap</param>
        /// <returns>The wrapper</returns>
        public static ITypeInfo Wrap(Type type)
        {
            return new ReflectionTypeInfo(type);
        }

        class ReflectionAttributeInfo : IAttributeInfo
        {
            readonly Attribute attribute;

            public ReflectionAttributeInfo(Attribute attribute)
            {
                this.attribute = attribute;
            }

            public T GetInstance<T>() where T : Attribute
            {
                return (T)attribute;
            }

            public TValue GetPropertyValue<TValue>(string propertyName)
            {
                PropertyInfo propInfo = attribute.GetType().GetProperty(propertyName);
                if (propInfo == null)
                    throw new ArgumentException("Could not find property " + propertyName + " on instance of " + attribute.GetType().FullName, "propertyName");

                return (TValue)propInfo.GetValue(attribute, new object[0]);
            }

            public override string ToString()
            {
                return attribute.ToString();
            }
        }

        class ReflectionMethodInfo : IMethodInfo
        {
            readonly MethodInfo method;

            public ReflectionMethodInfo(MethodInfo method)
            {
                this.method = method;
            }

            public bool IsAbstract
            {
                get { return method.IsAbstract; }
            }

            public bool IsStatic
            {
                get { return method.IsStatic; }
            }

            public MethodInfo MethodInfo
            {
                get { return method; }
            }

            public string Name
            {
                get { return method.Name; }
            }

            public string ReturnType
            {
                get { return method.ReturnType.FullName; }
            }

            public string TypeName
            {
                get { return method.ReflectedType.FullName; }
            }

            public object CreateInstance()
            {
                return Activator.CreateInstance(method.ReflectedType);
            }

            public IEnumerable<IAttributeInfo> GetCustomAttributes(Type attributeType)
            {
                foreach (Attribute attribute in method.GetCustomAttributes(attributeType, false))
                    yield return Wrap(attribute);
            }

            public bool HasAttribute(Type attributeType)
            {
                return method.IsDefined(attributeType, false);
            }

            public void Invoke(object testClass, params object[] parameters)
            {
                try
                {
                    method.Invoke(testClass, parameters);
                }
                catch (TargetParameterCountException)
                {
                    throw new ParamterCountMismatchException();
                }
                catch (TargetInvocationException ex)
                {
                    ExceptionUtility.RethrowWithNoStackTraceLoss(ex.InnerException);
                }
            }

            public override bool Equals(object obj)
            {
                ReflectionMethodInfo other = obj as ReflectionMethodInfo;
                if (other == null)
                    return false;

                return other.method == this.method;
            }

            public override int GetHashCode()
            {
                return method.GetHashCode();
            }

            public override string ToString()
            {
                return method.ToString();
            }
        }

        class ReflectionTypeInfo : ITypeInfo
        {
            const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
            readonly Type type;

            public ReflectionTypeInfo(Type type)
            {
                this.type = type;
            }

            public bool IsAbstract
            {
                get { return type.IsAbstract; }
            }

            public bool IsSealed
            {
                get { return type.IsSealed; }
            }

            public Type Type
            {
                get { return type; }
            }

            public IEnumerable<IAttributeInfo> GetCustomAttributes(Type attributeType)
            {
                foreach (Attribute attribute in type.GetCustomAttributes(attributeType, true))
                    yield return Wrap(attribute);
            }

            public IMethodInfo GetMethod(string methodName)
            {
                MethodInfo method = type.GetMethod(methodName, bindingFlags);
                if (method == null)
                    return null;

                return Wrap(method);
            }

            public IEnumerable<IMethodInfo> GetMethods()
            {
                foreach (MethodInfo method in type.GetMethods(bindingFlags))
                    yield return Wrap(method);
            }

            public bool HasAttribute(Type attributeType)
            {
                return type.IsDefined(attributeType, true);
            }

            public bool HasInterface(Type interfaceType)
            {
                foreach (Type implementedInterfaceType in type.GetInterfaces())
                    if (implementedInterfaceType == interfaceType)
                        return true;

                return false;
            }

            public override string ToString()
            {
                return type.ToString();
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.