本文整理汇总了C#中ICSharpCode.NRefactory.TypeSystem.ParameterizedType类的典型用法代码示例。如果您正苦于以下问题:C# ParameterizedType类的具体用法?C# ParameterizedType怎么用?C# ParameterizedType使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ParameterizedType类属于ICSharpCode.NRefactory.TypeSystem命名空间,在下文中一共展示了ParameterizedType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImportOpenGenericType
public void ImportOpenGenericType()
{
// class C<T, U> { void M<X>() {} }
var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T"));
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U"));
var m = new DefaultUnresolvedMethod(c, "M");
m.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.Method, 0, "X"));
c.Members.Add(m);
var resolvedC1 = TypeSystemHelper.CreateCompilationAndResolve(c);
var resolvedM1 = resolvedC1.Methods.Single(method => method.Name == "M");
var resolvedC2 = TypeSystemHelper.CreateCompilationAndResolve(c);
var resolvedM2 = resolvedC2.Methods.Single(method => method.Name == "M");
// the types, methods and type parameters differ in the two compilations:
Assert.AreNotEqual(resolvedC1, resolvedC2);
Assert.AreNotEqual(resolvedM1, resolvedM2);
Assert.AreNotEqual(resolvedC1.TypeParameters[1], resolvedC2.TypeParameters[1]);
Assert.AreNotEqual(resolvedM1.TypeParameters[0], resolvedM2.TypeParameters[0]);
// C<U, X>
var pt1 = new ParameterizedType(resolvedC1, new[] { resolvedC1.TypeParameters[1], resolvedM1.TypeParameters[0] });
var pt2 = (ParameterizedType)resolvedC2.Compilation.Import(pt1);
// importing resulted in C<U, X> in the new compilation:
Assert.AreEqual(resolvedC2, pt2.GetDefinition());
Assert.AreEqual(resolvedC2.TypeParameters[1], pt2.TypeArguments[0]);
Assert.AreEqual(resolvedM2.TypeParameters[0], pt2.TypeArguments[1]);
}
示例2: ResolveKnownBaseType
/// <summary>
/// Finds IList<T> or IEnumerable<T> base type.
/// </summary>
/// <param name="fullNamePrefix">Type code to search for (IList<T> or IEnumerable<T>)</param></param>
/// <param name="implementation">Found implementation.</param>
/// <param name="itemType">The only generic argument of <paramref name="implementation"/></param>
/// <returns>True if found, false otherwise.</returns>
private static bool ResolveKnownBaseType(this IType type, KnownTypeCode knownTypeCode, out ParameterizedType implementation, out IType itemType)
{
if (type == null) throw new ArgumentNullException("type");
implementation = null;
itemType = null;
ParameterizedType impl =
type.GetAllBaseTypes().OfType<ParameterizedType>().
Where(t => t.IsKnownType(knownTypeCode) && t.TypeParameterCount == 1)
.FirstOrDefault();
if (impl != null) {
implementation = impl;
itemType = impl.GetTypeArgument(0);
return true;
}
return false;
}
示例3: EnumerableToArrayInContravariantType
public void EnumerableToArrayInContravariantType()
{
ITypeParameter tp = new DefaultTypeParameter(EntityType.Method, 0, "T");
IType stringType = KnownTypeReference.String.Resolve(ctx);
ITypeDefinition enumerableType = ctx.GetTypeDefinition(typeof(IEnumerable<>));
ITypeDefinition comparerType = ctx.GetTypeDefinition(typeof(IComparer<>));
var comparerOfIEnumerableOfString = new ParameterizedType(comparerType, new [] { new ParameterizedType(enumerableType, new [] { stringType} ) });
var comparerOfTpArray = new ParameterizedType(comparerType, new [] { new ArrayType(tp) });
bool success;
Assert.AreEqual(
new [] { stringType },
ti.InferTypeArguments(new [] { tp },
new [] { new ResolveResult(comparerOfIEnumerableOfString) },
new [] { comparerOfTpArray },
out success));
Assert.IsTrue(success);
}
示例4: GetGenericNestedTypeOfBoundGenericClass
public void GetGenericNestedTypeOfBoundGenericClass()
{
// class A<X> { class B<Y> { } }
DefaultUnresolvedTypeDefinition a = new DefaultUnresolvedTypeDefinition(string.Empty, "A");
a.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.TypeDefinition, 0, "X"));
DefaultUnresolvedTypeDefinition b = new DefaultUnresolvedTypeDefinition(a, "B");
b.TypeParameters.Add(a.TypeParameters[0]);
b.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.TypeDefinition, 1, "Y"));
a.NestedTypes.Add(b);
var compilation = TypeSystemHelper.CreateCompilation(a, b);
ITypeDefinition resolvedA = compilation.MainAssembly.GetTypeDefinition(a.FullTypeName);
ITypeDefinition resolvedB = compilation.MainAssembly.GetTypeDefinition(b.FullTypeName);
// A<> gets self-parameterized, B<> stays unbound
Assert.AreEqual("A`1+B`1[[`0],[]]", resolvedA.GetNestedTypes().Single().ReflectionName);
ParameterizedType pt = new ParameterizedType(resolvedA, new [] { compilation.FindType(KnownTypeCode.String) });
Assert.AreEqual("A`1+B`1[[System.String],[]]", pt.GetNestedTypes().Single().ReflectionName);
}
示例5: IsGenericInterfaceImplementedByArray
static bool IsGenericInterfaceImplementedByArray(ParameterizedType rt)
{
if (rt == null || rt.TypeParameterCount != 1)
return false;
switch (rt.GetDefinition().FullName) {
case "System.Collections.Generic.IEnumerable":
case "System.Collections.Generic.ICollection":
case "System.Collections.Generic.IList":
case "System.Collections.Generic.IReadOnlyList":
return true;
default:
return false;
}
}
示例6: ListOfNSSystem
public void ListOfNSSystem()
{
var type = new ParameterizedType(compilation.FindType(typeof(List<>)).GetDefinition(), new[] { systemClass });
Assert.AreEqual("List<NS.System>", TypeToString(type));
Assert.AreEqual("List<System>", TypeToString(type, systemClass));
}
示例7: LookInCurrentUsingScope
ResolveResult LookInCurrentUsingScope(string identifier, IList<IType> typeArguments, bool isInUsingDeclaration, bool parameterizeResultType)
{
int k = typeArguments.Count;
// look in current namespace definitions
ResolvedUsingScope currentUsingScope = this.CurrentUsingScope;
for (ResolvedUsingScope u = currentUsingScope; u != null; u = u.Parent) {
INamespace n = u.Namespace;
// first look for a namespace
if (k == 0 && n != null) {
INamespace childNamespace = n.GetChildNamespace(identifier);
if (childNamespace != null) {
if (u.HasAlias(identifier))
return new AmbiguousTypeResolveResult(new UnknownType(null, identifier));
return new NamespaceResolveResult(childNamespace);
}
}
// then look for a type
if (n != null) {
ITypeDefinition def = n.GetTypeDefinition(identifier, k);
if (def != null) {
IType result = def;
if (parameterizeResultType) {
result = new ParameterizedType(def, typeArguments);
}
if (u.HasAlias(identifier))
return new AmbiguousTypeResolveResult(result);
else
return new TypeResolveResult(result);
}
}
// then look for aliases:
if (k == 0) {
if (u.ExternAliases.Contains(identifier)) {
return ResolveExternAlias(identifier);
}
if (!(isInUsingDeclaration && u == currentUsingScope)) {
foreach (var pair in u.UsingAliases) {
if (pair.Key == identifier) {
return pair.Value;
}
}
}
}
// finally, look in the imported namespaces:
if (!(isInUsingDeclaration && u == currentUsingScope)) {
IType firstResult = null;
foreach (var importedNamespace in u.Usings) {
ITypeDefinition def = importedNamespace.GetTypeDefinition(identifier, k);
if (def != null) {
if (firstResult == null) {
if (parameterizeResultType && k > 0)
firstResult = new ParameterizedType(def, typeArguments);
else
firstResult = def;
} else {
return new AmbiguousTypeResolveResult(firstResult);
}
}
}
if (firstResult != null)
return new TypeResolveResult(firstResult);
}
// if we didn't find anything: repeat lookup with parent namespace
}
return null;
}
示例8: IEnumerableCovarianceWithDynamic
public void IEnumerableCovarianceWithDynamic()
{
ITypeParameter tp = new DefaultTypeParameter(compilation, SymbolKind.Method, 0, "T");
var ienumerableOfT = new ParameterizedType(compilation.FindType(typeof(IEnumerable<>)).GetDefinition(), new[] { tp });
var ienumerableOfString = compilation.FindType(typeof(IEnumerable<string>));
var ienumerableOfDynamic = compilation.FindType(typeof(IEnumerable<ReflectionHelper.Dynamic>));
// static T M<T>(IEnumerable<T> x, IEnumerable<T> y) {}
// M(IEnumerable<dynamic>, IEnumerable<string>); -> should infer T=dynamic, no ambiguity
// See http://blogs.msdn.com/b/cburrows/archive/2010/04/01/errata-dynamic-conversions-and-overload-resolution.aspx
// for details.
bool success;
Assert.AreEqual(
new [] { SpecialType.Dynamic },
ti.InferTypeArguments(
new [] { tp },
new [] { new ResolveResult(ienumerableOfDynamic), new ResolveResult(ienumerableOfString) },
new [] { ienumerableOfT, ienumerableOfT },
out success));
Assert.IsTrue(success);
}
示例9: VisitParameterizedType
public override IType VisitParameterizedType(ParameterizedType type)
{
IType newType = base.VisitParameterizedType(type);
if (newType != type && ConstraintsValid) {
// something was changed, so we need to validate the constraints
ParameterizedType newParameterizedType = newType as ParameterizedType;
if (newParameterizedType != null) {
// C# 4.0 spec: §4.4.4 Satisfying constraints
var typeParameters = newParameterizedType.GetDefinition().TypeParameters;
for (int i = 0; i < typeParameters.Count; i++) {
ITypeParameter tp = typeParameters[i];
IType typeArg = newParameterizedType.GetTypeArgument(i);
switch (typeArg.Kind) { // void, null, and pointers cannot be used as type arguments
case TypeKind.Void:
case TypeKind.Null:
case TypeKind.Pointer:
ConstraintsValid = false;
break;
}
if (tp.HasReferenceTypeConstraint) {
if (typeArg.IsReferenceType != true)
ConstraintsValid = false;
}
if (tp.HasValueTypeConstraint) {
if (!NullableType.IsNonNullableValueType(typeArg))
ConstraintsValid = false;
}
if (tp.HasDefaultConstructorConstraint) {
ITypeDefinition def = typeArg.GetDefinition();
if (def != null && def.IsAbstract)
ConstraintsValid = false;
ConstraintsValid &= typeArg.GetConstructors(
m => m.Parameters.Count == 0 && m.Accessibility == Accessibility.Public,
GetMemberOptions.IgnoreInheritedMembers | GetMemberOptions.ReturnMemberDefinitions
).Any();
}
foreach (IType constraintType in tp.DirectBaseTypes) {
IType c = constraintType.AcceptVisitor(newParameterizedType.GetSubstitution());
ConstraintsValid &= conversions.IsConstraintConvertible(typeArg, c);
}
}
}
}
return newType;
}
示例10: LookInUsingScopeNamespace
ResolveResult LookInUsingScopeNamespace(ResolvedUsingScope usingScope, INamespace n, string identifier, IList<IType> typeArguments, bool parameterizeResultType)
{
if (n == null)
return null;
// first look for a namespace
int k = typeArguments.Count;
if (k == 0) {
INamespace childNamespace = n.GetChildNamespace(identifier);
if (childNamespace != null) {
if (usingScope != null && usingScope.HasAlias(identifier))
return new AmbiguousTypeResolveResult(new UnknownType(null, identifier));
return new NamespaceResolveResult(childNamespace);
}
}
// then look for a type
ITypeDefinition def = n.GetTypeDefinition(identifier, k);
if (def != null) {
IType result = def;
if (parameterizeResultType && k > 0) {
result = new ParameterizedType(def, typeArguments);
}
if (usingScope != null && usingScope.HasAlias(identifier))
return new AmbiguousTypeResolveResult(result);
else
return new TypeResolveResult(result);
}
return null;
}
示例11: VisitParameterizedType
public override IType VisitParameterizedType(ParameterizedType type)
{
IType newType = base.VisitParameterizedType(type);
if (newType != type && ConstraintsValid) {
// something was changed, so we need to validate the constraints
ParameterizedType newParameterizedType = newType as ParameterizedType;
if (newParameterizedType != null) {
// C# 4.0 spec: §4.4.4 Satisfying constraints
var typeParameters = newParameterizedType.GetDefinition().TypeParameters;
for (int i = 0; i < typeParameters.Count; i++) {
ITypeParameter tp = typeParameters[i];
IType typeArg = newParameterizedType.TypeArguments[i];
if (tp.HasReferenceTypeConstraint) {
if (typeArg.IsReferenceType != true)
ConstraintsValid = false;
}
if (tp.HasValueTypeConstraint) {
if (typeArg.IsReferenceType != false)
ConstraintsValid = false;
if (NullableType.IsNullable(typeArg))
ConstraintsValid = false;
}
if (tp.HasDefaultConstructorConstraint) {
ITypeDefinition def = typeArg.GetDefinition();
if (def != null && def.IsAbstract)
ConstraintsValid = false;
ConstraintsValid &= typeArg.GetConstructors(
overloadResolution.context,
m => m.Parameters.Count == 0 && m.Accessibility == Accessibility.Public
).Any();
}
foreach (IType constraintType in tp.Constraints) {
IType c = newParameterizedType.SubstituteInType(constraintType);
ConstraintsValid &= overloadResolution.IsConstraintConvertible(typeArg, c);
}
}
}
}
return newType;
}
示例12: NestedTypeInDerivedClass
public void NestedTypeInDerivedClass()
{
var type1 = new ParameterizedType(nestedClass, new[] { derivedClass.TypeParameters[0], compilation.FindType(KnownTypeCode.String) });
// short form "Nested<string>" cannot be used as it would refer to "Base<S>.Nested<string>"
Assert.AreEqual("Base<T>.Nested<string>", TypeToString(type1, derivedClass));
var type2 = new ParameterizedType(nestedClass, new[] { derivedClass.TypeParameters[1], compilation.FindType(KnownTypeCode.String) });
Assert.AreEqual("Nested<string>", TypeToString(type2, derivedClass));
}
示例13: SiblingClass
public void SiblingClass()
{
var type = new ParameterizedType(siblingClass, new[] { baseClass.TypeParameters[0] });
Assert.AreEqual("Sibling", TypeToString(type, nestedClass));
}
示例14: NestedType
public void NestedType()
{
var type = new ParameterizedType(nestedClass, new[] { compilation.FindType(KnownTypeCode.Char), compilation.FindType(KnownTypeCode.String) });
Assert.AreEqual("Base<char>.Nested<string>", TypeToString(type));
// The short form "Nested<string>" refers to "Base<T>.Nested<string>",
// so we need to use the long form to specify that T=char.
Assert.AreEqual("Base<char>.Nested<string>", TypeToString(type, baseClass));
Assert.AreEqual("Base<char>.Nested<string>", TypeToString(type, nestedClass));
Assert.AreEqual("Base<char>.Nested<string>", TypeToString(type, derivedClass));
}
示例15: AliasedTypeWrongTypeArgument
public void AliasedTypeWrongTypeArgument()
{
var type = new ParameterizedType(compilation.FindType(typeof(List<>)).GetDefinition(), new[] { compilation.FindType(KnownTypeCode.Int32) });
Assert.AreEqual("List<int>", TypeToString(type, systemClass));
}