1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System
.Reflection
;
6 using System
.Runtime
.CompilerServices
;
10 public struct ModuleHandle
12 readonly IntPtr
value;
14 public static readonly ModuleHandle EmptyHandle
= new ModuleHandle (IntPtr
.Zero
);
16 internal ModuleHandle (IntPtr v
)
21 internal IntPtr Value
{
27 public int MDStreamVersion
{
29 if (value == IntPtr
.Zero
)
30 throw new ArgumentNullException (String
.Empty
, "Invalid handle");
31 return RuntimeModule
.GetMDStreamVersion (value);
35 public RuntimeFieldHandle
ResolveFieldHandle (int fieldToken
)
37 return ResolveFieldHandle (fieldToken
, null, null);
40 public RuntimeMethodHandle
ResolveMethodHandle (int methodToken
)
42 return ResolveMethodHandle (methodToken
, null, null);
45 public RuntimeTypeHandle
ResolveTypeHandle (int typeToken
)
47 return ResolveTypeHandle (typeToken
, null, null);
50 static IntPtr
[]? ptrs_from_handles (RuntimeTypeHandle
[]? handles
)
55 var res
= new IntPtr
[handles
.Length
];
56 for (int i
= 0; i
< handles
.Length
; ++i
)
57 res
[i
] = handles
[i
].Value
;
61 public RuntimeTypeHandle
ResolveTypeHandle (int typeToken
, RuntimeTypeHandle
[]? typeInstantiationContext
, RuntimeTypeHandle
[]? methodInstantiationContext
)
63 ResolveTokenError error
;
64 if (value == IntPtr
.Zero
)
65 throw new ArgumentNullException (String
.Empty
, "Invalid handle");
66 IntPtr res
= RuntimeModule
.ResolveTypeToken (value, typeToken
, ptrs_from_handles (typeInstantiationContext
), ptrs_from_handles (methodInstantiationContext
), out error
);
67 if (res
== IntPtr
.Zero
)
68 throw new TypeLoadException (String
.Format ("Could not load type '0x{0:x}' from assembly '0x{1:x}'", typeToken
, value.ToInt64 ()));
70 return new RuntimeTypeHandle (res
);
73 public RuntimeMethodHandle
ResolveMethodHandle (int methodToken
, RuntimeTypeHandle
[]? typeInstantiationContext
, RuntimeTypeHandle
[]? methodInstantiationContext
)
75 ResolveTokenError error
;
76 if (value == IntPtr
.Zero
)
77 throw new ArgumentNullException (String
.Empty
, "Invalid handle");
78 IntPtr res
= RuntimeModule
.ResolveMethodToken (value, methodToken
, ptrs_from_handles (typeInstantiationContext
), ptrs_from_handles (methodInstantiationContext
), out error
);
79 if (res
== IntPtr
.Zero
)
80 throw new Exception (String
.Format ("Could not load method '0x{0:x}' from assembly '0x{1:x}'", methodToken
, value.ToInt64 ()));
82 return new RuntimeMethodHandle (res
);
85 public RuntimeFieldHandle
ResolveFieldHandle (int fieldToken
, RuntimeTypeHandle
[]? typeInstantiationContext
, RuntimeTypeHandle
[]? methodInstantiationContext
)
87 ResolveTokenError error
;
88 if (value == IntPtr
.Zero
)
89 throw new ArgumentNullException (String
.Empty
, "Invalid handle");
91 IntPtr res
= RuntimeModule
.ResolveFieldToken (value, fieldToken
, ptrs_from_handles (typeInstantiationContext
), ptrs_from_handles (methodInstantiationContext
), out error
);
92 if (res
== IntPtr
.Zero
)
93 throw new Exception (String
.Format ("Could not load field '0x{0:x}' from assembly '0x{1:x}'", fieldToken
, value.ToInt64 ()));
95 return new RuntimeFieldHandle (res
);
98 public RuntimeFieldHandle
GetRuntimeFieldHandleFromMetadataToken (int fieldToken
)
100 return ResolveFieldHandle (fieldToken
);
103 public RuntimeMethodHandle
GetRuntimeMethodHandleFromMetadataToken (int methodToken
)
105 return ResolveMethodHandle (methodToken
);
108 public RuntimeTypeHandle
GetRuntimeTypeHandleFromMetadataToken (int typeToken
)
110 return ResolveTypeHandle (typeToken
);
113 public override bool Equals (object? obj
)
115 if (obj
== null || GetType () != obj
.GetType ())
118 return value == ((ModuleHandle
)obj
).Value
;
121 public bool Equals (ModuleHandle handle
)
123 return value == handle
.Value
;
126 public override int GetHashCode ()
128 return value.GetHashCode ();
131 public static bool operator == (ModuleHandle left
, ModuleHandle right
)
133 return Equals (left
, right
);
136 public static bool operator != (ModuleHandle left
, ModuleHandle right
)
138 return !Equals (left
, right
);