Datei: NDODLL/Metaclasses.cs

Last Commit (9de9fa9)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Collections.Generic;
25 using System.Reflection;
26 using NDO.Configuration;
27
28 namespace NDO
29 {
30 ····/// <summary>
31 ····/// Summary description for Metaclasses.
32 ····/// </summary>
33 ····internal class Metaclasses
34 ····{
35 private static Dictionary<Type, IMetaClass> theClasses = new Dictionary<Type, IMetaClass>( ) ;
36
37 internal static IMetaClass GetClass( Type t, INDOContainer configContainer)
38 ········{
39 ············if (t.IsGenericTypeDefinition)
40 ················return null;
41
42 IMetaClass mc;
43
44 ············if (!theClasses.TryGetValue( t, out mc ))
45 ············{
46 ················lock (theClasses)
47 ················{
48 ····················if (!theClasses.TryGetValue( t, out mc ))··// Threading double check
49 ····················{
50 ························Type mcType = t.GetNestedType( "MetaClass", BindingFlags.NonPublic | BindingFlags.Public );
51 ························if (null == mcType)
52 ····························throw new NDOException( 13, "Missing nested class 'MetaClass' for type '" + t.Name + "'; the type doesn't seem to be enhanced." );
53 ························Type t2 = mcType;
54 ························if (t2.IsGenericTypeDefinition)
55 ····························t2 = t2.MakeGenericType( t.GetGenericArguments() );
56 var innerMc = ( IMetaClass) Activator. CreateInstance( t2 ) ;
57 mc = new NDOMetaclass( t, innerMc, configContainer ) ;
 
 
 
 
 
58 ························theClasses.Add( t, mc );
59 ····················}
60 ················}
61 ············}
62
63 ············return mc;
64 ········}
65
66 ········internal static IMetaClass GetInnerClass( Type t )
67 ········{
68 ············if (t.IsGenericTypeDefinition)
69 ················return null;
70
71 ············IMetaClass mc;
72
73 ············if (!theClasses.TryGetValue( t, out mc ))
74 ············{
75 ················Type mcType = t.GetNestedType( "MetaClass", BindingFlags.NonPublic | BindingFlags.Public );
76 ················if (null == mcType)
77 ····················throw new NDOException( 13, "Missing nested class 'MetaClass' for type '" + t.Name + "'; the type doesn't seem to be enhanced." );
78 ················Type t2 = mcType;
79 ················if (t2.IsGenericTypeDefinition)
80 ····················t2 = t2.MakeGenericType( t.GetGenericArguments() );
81 ················var innerMc = (IMetaClass)Activator.CreateInstance( t2 );
82 ················return innerMc;
83 ············}
84
85 ············return mc;
86 ········}
87 ····}
88 }
89
New Commit (a99de3b)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Collections.Generic;
25 using System.Reflection;
26 using NDO.Configuration;
27
28 namespace NDO
29 {
30 ····/// <summary>
31 ····/// Summary description for Metaclasses.
32 ····/// </summary>
33 ····internal class Metaclasses
34 ····{
35 private static Dictionary<Type, IMetaClass2> theClasses = new Dictionary<Type, IMetaClass2>( ) ;
36
37 internal static IMetaClass2 GetClass( Type t )
38 ········{
39 ············if (t.IsGenericTypeDefinition)
40 ················return null;
41
42 IMetaClass2 mc;
43
44 ············if (!theClasses.TryGetValue( t, out mc ))
45 ············{
46 ················lock (theClasses)
47 ················{
48 ····················if (!theClasses.TryGetValue( t, out mc ))··// Threading double check
49 ····················{
50 ························Type mcType = t.GetNestedType( "MetaClass", BindingFlags.NonPublic | BindingFlags.Public );
51 ························if (null == mcType)
52 ····························throw new NDOException( 13, "Missing nested class 'MetaClass' for type '" + t.Name + "'; the type doesn't seem to be enhanced." );
53 ························Type t2 = mcType;
54 ························if (t2.IsGenericTypeDefinition)
55 ····························t2 = t2.MakeGenericType( t.GetGenericArguments() );
56 var o = Activator. CreateInstance( t2, t ) ;
57 if ( o is IMetaClass2 mc2)
58 ····························mc = mc2;
59 ························else if (o is IMetaClass mc1)
60 ····························mc = new NDOMetaclass( t, mc1 );
61 ························else
62 ····························throw new NDOException( 101010, $"MetaClass for type '{t.FullName}' must implement IMetaClass or IMetaClass2, but doesn't." );
63 ························theClasses.Add( t, mc );
64 ····················}
65 ················}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66 ············}
67
68 ············return mc;
69 ········}
70 ····}
71 }
72