PageRenderTime 20ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/Builders/ProviderInfo.cs

#
C# | 91 lines | 69 code | 14 blank | 8 comment | 10 complexity | 6cb8625197a8f98416a44218c1e025a7 MD5 | raw file
 1// ****************************************************************
 2// Copyright 2008, Charlie Poole
 3// This is free software licensed under the NUnit license. You may
 4// obtain a copy of the license at http://nunit.org.
 5// ****************************************************************
 6
 7using System;
 8using System.Collections;
 9using System.Reflection;
10
11namespace NUnit.Core.Builders
12{
13    class ProviderReference
14    {
15        private Type providerType;
16        private string providerName;
17        private object[] providerArgs;
18
19        public ProviderReference(Type providerType, string providerName)
20        {
21            if (providerType == null)
22                throw new ArgumentNullException("providerType");
23            if (providerName == null)
24                throw new ArgumentNullException("providerName");
25
26            this.providerType = providerType;
27            this.providerName = providerName;
28        }
29
30        public ProviderReference(Type providerType, object[] args, string providerName)
31            : this(providerType, providerName)
32        {
33            this.providerArgs = args;
34        }
35
36        public string Name
37        {
38            get { return this.providerName; }
39        }
40
41        public IEnumerable GetInstance()
42        {
43                MemberInfo[] members = providerType.GetMember(
44                    providerName,
45                    MemberTypes.Field | MemberTypes.Method | MemberTypes.Property,
46                    BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
47
48                if (members.Length == 0)
49                    throw new Exception(string.Format(
50                        "Unable to locate {0}.{1}", providerType.FullName, providerName));
51
52                return (IEnumerable)GetProviderObjectFromMember(members[0]);
53        }
54
55        private object GetProviderObjectFromMember(MemberInfo member)
56        {
57            object providerObject = null;
58            object instance = null;
59
60            switch (member.MemberType)
61            {
62                case MemberTypes.Property:
63                    PropertyInfo providerProperty = member as PropertyInfo;
64                    MethodInfo getMethod = providerProperty.GetGetMethod(true);
65                    if (!getMethod.IsStatic)
66                        //instance = ProviderCache.GetInstanceOf(providerType);
67                        instance = Reflect.Construct(providerType, providerArgs);
68                    providerObject = providerProperty.GetValue(instance, null);
69                    break;
70
71                case MemberTypes.Method:
72                    MethodInfo providerMethod = member as MethodInfo;
73                    if (!providerMethod.IsStatic)
74                        //instance = ProviderCache.GetInstanceOf(providerType);
75                        instance = Reflect.Construct(providerType, providerArgs);
76                    providerObject = providerMethod.Invoke(instance, null);
77                    break;
78
79                case MemberTypes.Field:
80                    FieldInfo providerField = member as FieldInfo;
81                    if (!providerField.IsStatic)
82                        //instance = ProviderCache.GetInstanceOf(providerType);
83                        instance = Reflect.Construct(providerType, providerArgs);
84                    providerObject = providerField.GetValue(instance);
85                    break;
86            }
87
88            return providerObject;
89        }
90    }
91}