public void GetHashCode_ForEqualObjects_WithUsedGenericParameters() { var signature1 = MethodSignature.Create(_genericMethod1); var signature2 = MethodSignature.Create(_genericMethod2); Assert.That(signature1.GetHashCode(), Is.EqualTo(signature2.GetHashCode())); }
public void Create_NonGenericMethod() { var method = NormalizingMemberInfoFromExpressionUtility.GetMethod((object obj) => obj.Equals(null)); var signature = MethodSignature.Create(method); Assert.That(signature.ReturnType, Is.SameAs(typeof(bool))); Assert.That(signature.GenericParameterCount, Is.EqualTo(0)); Assert.That(signature.ParameterTypes, Is.EqualTo(new[] { typeof(object) })); }
public void Equals_WithUsedGenericParameters() { var signature1 = MethodSignature.Create(_genericMethod1); var signature2 = MethodSignature.Create(_genericMethod2); var signature3 = MethodSignature.Create(_genericMethod3); Assert.That(signature1.Equals(signature2), Is.True); Assert.That(signature1.Equals(signature3), Is.False); }
public void Create_ClosedGenericMethod() { var method = _genericMethod1.MakeGenericMethod(typeof(int), typeof(int)); Assert.That( () => MethodSignature.Create(method), Throws.ArgumentException .With.ArgumentExceptionMessageEqualTo( "Closed generic methods are not supported.", "methodBase")); }
public void Create_OpenGenericMethod() { var signature = MethodSignature.Create(_genericMethod1); Assert.That(signature.ReturnType, Is.SameAs(_genericMethod1.ReturnType)); Assert.That(signature.GenericParameterCount, Is.EqualTo(2)); var parameter = _genericMethod1.GetParameters().Single(); Assert.That(signature.ParameterTypes, Is.EqualTo(new[] { parameter.ParameterType })); }
public void GetMostDerivedVirtualMethod_DerivedTypeMethod_NonMatchingSignature() { var method = NormalizingMemberInfoFromExpressionUtility.GetMethod((DomainType obj) => obj.DerivedTypeMethod()); var signature = new MethodSignature(typeof(int), Type.EmptyTypes, 0); Assert.That(signature, Is.Not.EqualTo(MethodSignature.Create(method))); var result = _finder.GetMostDerivedVirtualMethod(method.Name, signature, _typeToStartSearch); Assert.That(result, Is.Null); }
private static void InspectType( AssemblyDefinition assembly, TypeDefinition type, IDictionary <string, XElement> documentation, ICollection <CakeScriptAlias> result) { if (type.IsStatic()) { foreach (var method in type.Methods) { if (IsCakeAlias(method, out bool isPropertyAlias)) { if (!IsValidCakeAlias(method, isPropertyAlias)) { continue; } // Create the alias. var alias = new CakeScriptAlias() { Method = MethodSignature.Create(method), Obsolete = method.GetObsoleteAttribute(), Type = isPropertyAlias ? ScriptAliasType.Property : ScriptAliasType.Method, Name = method.Name, Namespaces = new HashSet <string>(StringComparer.Ordinal) }; // Cached property alias? if (alias.Type == ScriptAliasType.Property) { alias.Cached = IsCakePropertyAliasCached(method); } // Get documentation. if (documentation.TryGetValue(alias.Method.CRef, out XElement element)) { alias.Documentation = element; } // Get namespaces. alias.Namespaces.Add(type.Namespace); alias.Namespaces.AddRange(assembly.GetCakeNamespaces()); alias.Namespaces.AddRange(type.GetCakeNamespaces()); alias.Namespaces.AddRange(method.GetCakeNamespaces()); // Add it to the results. result.Add(alias); } } } }
public void ToString_GenericMethod() { var signature1 = MethodSignature.Create(_genericMethod1); Assert.That(signature1.ToString(), Is.EqualTo("[0]([1])`2")); var signature2 = MethodSignature.Create(_genericMethod2); Assert.That(signature2.ToString(), Is.EqualTo("[0]([1])`2")); var signature3 = MethodSignature.Create(_genericMethod3); Assert.That(signature3.ToString(), Is.EqualTo("[1]([0])`2")); }
private InterfaceMapping CreateForAdded(MutableType mutableType, Type interfaceType) { // Only public virtual methods may implicitly implement interfaces, ignore shadowed methods. (ECMA-335, 6th edition, II.12.2) var implementationCandidates = mutableType .GetAllMethods().Where(m => m.IsPublic && !m.IsStatic) // Optimization: Don't use GetMethods(BindingFlags). .Where(m => m.IsVirtual) .ToLookup(m => new { m.Name, Signature = MethodSignature.Create(m) }); var interfaceMethods = interfaceType.GetMethods(); var targetMethods = interfaceMethods .Select(m => GetMostDerivedOrDefault(implementationCandidates[new { m.Name, Signature = MethodSignature.Create(m) }])) .ToArray(); return(new InterfaceMapping { InterfaceType = interfaceType, TargetType = mutableType, InterfaceMethods = interfaceMethods, TargetMethods = targetMethods }); }
/// <inheritdoc /> public MethodInfo GetMostDerivedVirtualMethod(string name, MethodSignature signature, Type typeToStartSearch) { ArgumentUtility.CheckNotNullOrEmpty("name", name); ArgumentUtility.CheckNotNull("signature", signature); ArgumentUtility.CheckNotNull("typeToStartSearch", typeToStartSearch); Func <MethodInfo, bool> predicate = m => m.IsVirtual && m.Name == name && MethodSignature.Create(m).Equals(signature); return(FirstOrDefaultFromOrderedBaseMethods(typeToStartSearch, predicate)); }
private string BuildSignatureString(MethodBase method) { var methodSignature = MethodSignature.Create(method); return(methodSignature.ToString()); }
public MutableMethodInfo CreateMethod( MutableType declaringType, string name, MethodAttributes attributes, IEnumerable <GenericParameterDeclaration> genericParameters, Func <GenericParameterContext, Type> returnTypeProvider, Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > parameterProvider, Func <MethodBodyCreationContext, Expression> bodyProvider) { ArgumentUtility.CheckNotNull("declaringType", declaringType); ArgumentUtility.CheckNotNullOrEmpty("name", name); ArgumentUtility.CheckNotNull("genericParameters", genericParameters); ArgumentUtility.CheckNotNull("returnTypeProvider", returnTypeProvider); ArgumentUtility.CheckNotNull("parameterProvider", parameterProvider); // Body provider may be null (for abstract methods). // TODO 5478: virtual and static is an invalid combination var isAbstract = attributes.IsSet(MethodAttributes.Abstract); if (!isAbstract && bodyProvider == null) { throw new ArgumentNullException("bodyProvider", "Non-abstract methods must have a body."); } if (isAbstract && bodyProvider != null) { throw new ArgumentException("Abstract methods cannot have a body.", "bodyProvider"); } MemberAttributesUtility.ValidateAttributes("methods", MemberAttributesUtility.InvalidMethodAttributes, attributes, "attributes"); var isVirtual = attributes.IsSet(MethodAttributes.Virtual); var isNewSlot = attributes.IsSet(MethodAttributes.NewSlot); if (isAbstract && !isVirtual) { throw new ArgumentException("Abstract methods must also be virtual.", "attributes"); } if (!isVirtual && isNewSlot) { throw new ArgumentException("NewSlot methods must also be virtual.", "attributes"); } var methodItems = GetMethodSignatureItems(declaringType, genericParameters, returnTypeProvider, parameterProvider); var signature = new MethodSignature( methodItems.ReturnType, methodItems.ParameterDeclarations.Select(pd => pd.Type), methodItems.GenericParameters.Count, s_methodSignatureStringBuilderHelper); if (declaringType.AddedMethods.Any(m => m.Name == name && MethodSignature.Create(m).Equals(signature))) { throw new InvalidOperationException("Method with equal name and signature already exists."); } var baseMethod = GetBaseMethod(declaringType, name, signature, isVirtual, isNewSlot); // TODO 5478: if it is an implicit overriddenMethod override, it needs at least the same ore more public visibility var body = GetMethodBody(declaringType, attributes, bodyProvider, methodItems, baseMethod); return(new MutableMethodInfo( declaringType, name, attributes, methodItems.GenericParameters, methodItems.ReturnType, methodItems.ParameterDeclarations, baseMethod, body)); }
public MutableConstructorInfo CreateConstructor( MutableType declaringType, MethodAttributes attributes, IEnumerable <ParameterDeclaration> parameters, Func <ConstructorBodyCreationContext, Expression> bodyProvider) { ArgumentUtility.CheckNotNull("declaringType", declaringType); ArgumentUtility.CheckNotNull("parameters", parameters); ArgumentUtility.CheckNotNull("bodyProvider", bodyProvider); MemberAttributesUtility.ValidateAttributes("constructors", MemberAttributesUtility.InvalidConstructorAttributes, attributes, "attributes"); var isStatic = attributes.IsSet(MethodAttributes.Static); var paras = parameters.ToList(); if (isStatic && paras.Count != 0) { throw new ArgumentException("A type initializer (static constructor) cannot have parameters.", "parameters"); } var signature = new MethodSignature(typeof(void), paras.Select(p => p.Type), 0); if (declaringType.AddedConstructors.Any(ctor => ctor.IsStatic == isStatic && MethodSignature.Create(ctor).Equals(signature))) { throw new InvalidOperationException("Constructor with equal signature already exists."); } var parameterExpressions = paras.Select(p => p.Expression); var context = new ConstructorBodyCreationContext(declaringType, isStatic, parameterExpressions); var body = BodyProviderUtility.GetTypedBody(typeof(void), bodyProvider, context); var attr = attributes.Set(MethodAttributes.SpecialName | MethodAttributes.RTSpecialName); return(new MutableConstructorInfo(declaringType, attr, paras, body)); }
public void Create_ClosedGenericMethod() { var method = _genericMethod1.MakeGenericMethod(typeof(int), typeof(int)); MethodSignature.Create(method); }