本文整理汇总了C#中MetadataReader.GetNamespaceDefinition方法的典型用法代码示例。如果您正苦于以下问题:C# MetadataReader.GetNamespaceDefinition方法的具体用法?C# MetadataReader.GetNamespaceDefinition怎么用?C# MetadataReader.GetNamespaceDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MetadataReader
的用法示例。
在下文中一共展示了MetadataReader.GetNamespaceDefinition方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ValidateNamespaceChildren
/// <summary>
/// Helper method that will validate that a NamespaceDefinition (and all NamespaceDefinitions considered children
/// of it) report correct values for their child namespaces, types, etc. All namespaces in the module are expected
/// to be listed in the allNamespaces array. Additionally, the global namespace is expected to have type definitions
/// for GlobalClassA, GlobalClassB, and Module. No type forwarder declarations are expected.
///
/// All namespaces that aren't the global NS are expected to have type definitions equal to the array
/// @namespaceName.Split('.')
/// So, ns1.Ns2.NS3 is expected to have type definitions
/// {"ns1", "Ns2", "NS3"}.
///
/// definitionExceptions and forwarderExceptions may be used to override the default expectations. Pass in
/// namespace (key) and what is expected (list of strings) for each exception.
/// </summary>
private void ValidateNamespaceChildren(
MetadataReader reader,
NamespaceDefinitionHandle initHandle,
string[] allNamespaces,
IReadOnlyDictionary<string, IList<string>> definitionExceptions = null,
IReadOnlyDictionary<string, IList<string>> forwarderExceptions = null)
{
// Don't want to have to deal with null.
if (definitionExceptions == null)
{
definitionExceptions = new Dictionary<string, IList<string>>();
}
if (forwarderExceptions == null)
{
forwarderExceptions = new Dictionary<string, IList<string>>();
}
var rootNamespaceDefinition = reader.GetNamespaceDefinition(initHandle);
string rootNamespaceName = reader.GetString(initHandle);
// We need to be in the table of all namespaces...
Assert.Contains(rootNamespaceName, allNamespaces);
// Cool. Now check to make sure that .Name only returns the last bit of our namespace name.
var expNamespaceNameSegment = rootNamespaceName.Split('.').Last();
var rootNamespaceNameSegment = reader.GetString(rootNamespaceDefinition.Name);
Assert.Equal(expNamespaceNameSegment, rootNamespaceNameSegment);
bool isGlobalNamespace = rootNamespaceName.Length == 0;
string[] expTypeDefinitions = null;
// Special case: Global NS has GlobalClassA, GlobalClassB. Others just have autogenerated classes.
if (definitionExceptions.ContainsKey(rootNamespaceName))
{
expTypeDefinitions = definitionExceptions[rootNamespaceName].ToArray();
}
else if (isGlobalNamespace)
{
expTypeDefinitions = new[] { "GlobalClassA", "GlobalClassB", "<Module>" };
}
else
{
expTypeDefinitions = rootNamespaceName.Split('.');
}
// Validating type definitions inside the namespace...
int numberOfTypeDefinitions = 0;
foreach (var definitionHandle in rootNamespaceDefinition.TypeDefinitions)
{
var definition = reader.GetTypeDefinition(definitionHandle);
var definitionName = reader.GetString(definition.Name);
var definitionFullNamespaceName = reader.GetString(definition.Namespace);
Assert.Equal(rootNamespaceName, definitionFullNamespaceName);
Assert.Contains(definitionName, expTypeDefinitions);
numberOfTypeDefinitions += 1;
}
// Guarantee that there are no extra unexpected members...
Assert.Equal(numberOfTypeDefinitions, expTypeDefinitions.Length);
string[] expTypeForwarders = null;
if (forwarderExceptions.ContainsKey(rootNamespaceName))
{
expTypeForwarders = forwarderExceptions[rootNamespaceName].ToArray();
}
else
{
expTypeForwarders = new string[] { };
}
int numberOfTypeForwarders = 0;
foreach (var forwarderHandle in rootNamespaceDefinition.ExportedTypes)
{
var forwarder = reader.GetExportedType(forwarderHandle);
Assert.True(reader.StringComparer.Equals(forwarder.Namespace, rootNamespaceName));
var forwarderName = reader.GetString(forwarder.Name);
Assert.Contains(forwarderName, expTypeForwarders);
numberOfTypeForwarders += 1;
}
Assert.Equal(expTypeForwarders.Length, numberOfTypeForwarders);
// Validate sub-namespaces
// If the last index of '.' in a namespace name is == the current name's length, then
// that ns is a direct child of the current one!
IList<String> expChildren = null;
//.........这里部分代码省略.........
示例2: CompareNamespaceReferenceToDefinition
public static bool CompareNamespaceReferenceToDefinition(NamespaceReferenceHandle nr1, MetadataReader mr1, NamespaceDefinitionHandle nd2, MetadataReader mr2)
{
NamespaceReference nrData1 = mr1.GetNamespaceReference(nr1);
NamespaceDefinition ndData2 = mr2.GetNamespaceDefinition(nd2);
if (nrData1.Name.IsNull(mr1) != ndData2.Name.IsNull(mr2))
return false;
if (!nrData1.Name.IsNull(mr1))
{
if (!nrData1.Name.StringEquals(ndData2.Name.GetConstantStringValue(mr2).Value, mr1))
return false;
}
switch (nrData1.ParentScopeOrNamespace.HandleType)
{
case HandleType.NamespaceReference:
if (ndData2.ParentScopeOrNamespace.HandleType != HandleType.NamespaceDefinition)
return false;
return CompareNamespaceReferenceToDefinition(nrData1.ParentScopeOrNamespace.ToNamespaceReferenceHandle(mr1), mr1, ndData2.ParentScopeOrNamespace.ToNamespaceDefinitionHandle(mr2), mr2);
case HandleType.ScopeReference:
if (ndData2.ParentScopeOrNamespace.HandleType != HandleType.ScopeDefinition)
return false;
return CompareScopeReferenceToDefinition(nrData1.ParentScopeOrNamespace.ToScopeReferenceHandle(mr1), mr1, ndData2.ParentScopeOrNamespace.ToScopeDefinitionHandle(mr2), mr2);
default:
Debug.Assert(false);
throw new BadImageFormatException();
}
}