CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
GetConstructor.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="GetConstructor.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Reflection;
10using System.Linq;
11#if !NUNIT
12using System.Diagnostics;
13using Microsoft.VisualStudio.TestTools.UnitTesting;
14#else
15using NUnit.Framework;
16using TestClass = NUnit.Framework.TestFixtureAttribute;
17using TestInitialize = NUnit.Framework.SetUpAttribute;
18using TestCleanup = NUnit.Framework.TearDownAttribute;
19using TestMethod = NUnit.Framework.TestAttribute;
20#endif
21using Csla.Reflection;
22
24{
25 [TestClass]
27 {
28 [TestMethod]
30 {
31 var flags = System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
32 var obj = new HasStaticCtor();
33 var ctor = GetConstructor(obj.GetType(), flags, null, new Type[] { }, null);
34 Assert.IsNotNull(ctor);
35 Assert.IsTrue(ctor.IsStatic);
36 }
37
38 [TestMethod]
40 {
41 var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
42 var obj = new HasStaticCtor();
43 var ctor = GetConstructor(obj.GetType(), flags, null, new Type[] { }, null);
44 Assert.IsNotNull(ctor);
45 Assert.IsFalse(ctor.IsStatic);
46 }
47
48 private static ConstructorInfo GetConstructor(Type t, BindingFlags bindingFlags, object c, Type[] paramTypes, object d)
49 {
50 if (paramTypes.Length > 0)
51 throw new NotImplementedException();
52 var ti = t.GetTypeInfo();
53 var m = ti.DeclaredConstructors.Where(r => r.GetParameters().Count() == 0);
54 if (!((bindingFlags & BindingFlags.Instance) > 0))
55 m = m.Where(r => r.IsStatic);
56 if (!((bindingFlags & BindingFlags.Static) > 0))
57 m = m.Where(r => !r.IsStatic);
58 if (!((bindingFlags & BindingFlags.Public) > 0))
59 m = m.Where(r => !r.IsPublic);
60 if (!((bindingFlags & BindingFlags.NonPublic) > 0))
61 m = m.Where(r => r.IsPublic);
62 return m.FirstOrDefault();
63 }
64
65 }
66
67 public class HasStaticCtor
68 {
69 static HasStaticCtor()
70 { }
71 }
72}