Skip to content

Commit

Permalink
remove LINQ I
Browse files Browse the repository at this point in the history
  • Loading branch information
Sholtee committed Feb 24, 2022
1 parent 96d4546 commit 7d0cb28
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 220 deletions.
24 changes: 24 additions & 0 deletions SRC/Private/Extensions/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* *
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Collections.Generic;

namespace Solti.Utils.Proxy.Internals
Expand All @@ -23,5 +24,28 @@ internal static class IEnumerableExtensions
}

public static int? IndexOf<T>(this IEnumerable<T> src, T item) => src.IndexOf(item, EqualityComparer<T>.Default);

public static IReadOnlyList<TMeta> Convert<TMeta, TConcrete>(this IEnumerable<TConcrete> original, Func<TConcrete, TMeta> convert, Func<TConcrete, bool>? drop = null)
{
List<TMeta> lst = new();
foreach (TConcrete concrete in original)
{
if (drop?.Invoke(concrete) is true)
continue;

lst.Add(convert(concrete));
}
return lst;
}

public static bool Some<T>(this IEnumerable<T> src, Func<T, bool>? predicate = null)
{
foreach (T item in src)
{
if (predicate?.Invoke(item) is not false)
return true;
}
return false;
}
}
}
48 changes: 24 additions & 24 deletions SRC/Private/Extensions/Metadata/MemberInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
Expand Down Expand Up @@ -49,30 +48,31 @@ private static MemberInfo DoExtractFrom(LambdaExpression expr)

public static MemberInfo ExtractFrom(MethodInfo accessor, MemberTypes memberType, out MethodInfo method) // settert es esemenyt kifejezesekbol nem fejthetunk ki: https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0832
{
Instruction? call = accessor.GetInstructions().SingleOrDefault(instruction => instruction.OpCode == OpCodes.Callvirt);

if (call is not null)
foreach (Instruction instruction in accessor.GetInstructions())
{
method = (MethodInfo) call.Operand;

switch (memberType)
if (instruction.OpCode == OpCodes.Callvirt)
{
case MemberTypes.Property:
foreach (PropertyInfo prop in method.DeclaringType.GetProperties()) // nem hasznalhatunk SingleOrDefault()-ot mert lambdaban nem hivatkozhatunk by ref parametert [CS1628]
{
if (prop.SetMethod == method || prop.GetMethod == method)
return prop;
}
break;
case MemberTypes.Event:
foreach (EventInfo evt in method.DeclaringType.GetEvents())
{
if (evt.AddMethod == method || evt.RemoveMethod == method)
return evt;
}
break;
case MemberTypes.Method:
return method;
method = (MethodInfo) instruction.Operand;

switch (memberType)
{
case MemberTypes.Property:
foreach (PropertyInfo prop in method.DeclaringType.GetProperties()) // nem hasznalhatunk SingleOrDefault()-ot mert lambdaban nem hivatkozhatunk by ref parametert [CS1628]
{
if (prop.SetMethod == method || prop.GetMethod == method)
return prop;
}
break;
case MemberTypes.Event:
foreach (EventInfo evt in method.DeclaringType.GetEvents())
{
if (evt.AddMethod == method || evt.RemoveMethod == method)
return evt;
}
break;
case MemberTypes.Method:
return method;
}
}
}

Expand All @@ -83,7 +83,7 @@ private static MemberInfo DoExtractFrom(LambdaExpression expr)
// Explicit implementacional a nev "Nevter.Interface.Tag" formaban van
//

private static readonly Regex FStripper = new Regex("([\\w]+)$", RegexOptions.Compiled | RegexOptions.Singleline);
private static readonly Regex FStripper = new("([\\w]+)$", RegexOptions.Compiled | RegexOptions.Singleline);

public static string StrippedName(this MemberInfo self) => FStripper.Match(self.Name).Value;
}
Expand Down
5 changes: 2 additions & 3 deletions SRC/Private/Extensions/Metadata/MethodBaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Solti.Utils.Proxy.Internals
Expand All @@ -21,7 +20,7 @@ internal static class MethodBaseExtensions
_ when src.IsFamilyOrAssembly => AccessModifiers.Protected | AccessModifiers.Internal,
_ when src.IsFamilyAndAssembly => AccessModifiers.Protected | AccessModifiers.Private,
_ when src.IsPublic => AccessModifiers.Public,
_ when src.IsPrivate && src.GetImplementedInterfaceMethods().Any() => AccessModifiers.Explicit,
_ when src.IsPrivate && src.GetImplementedInterfaceMethods().Some() => AccessModifiers.Explicit,
_ when src.IsPrivate => AccessModifiers.Private,
#pragma warning disable CA2201 // In theory we should never reach here.
_ => throw new Exception(Resources.UNDETERMINED_ACCESS_MODIFIER)
Expand All @@ -32,7 +31,7 @@ internal static class MethodBaseExtensions
? Array.Empty<Type>()
: src
.GetImplementedInterfaceMethods()
.Select(m => m.ReflectedType);
.Convert(m => m.ReflectedType);

public static IEnumerable<MethodBase> GetImplementedInterfaceMethods(this MethodBase src)
{
Expand Down
7 changes: 3 additions & 4 deletions SRC/Private/Extensions/Metadata/ParameterInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Linq;
using System.Reflection;

namespace Solti.Utils.Proxy.Internals
Expand All @@ -18,7 +17,7 @@ public static ParameterKind GetParameterKind(this ParameterInfo src)
// mindig false (netcore3.0)
//

if (src.Position == -1) return src switch
if (src.Position is -1) return src switch
{
_ when src.ParameterType.IsByRef && IsReadOnly() => ParameterKind.RefReadonly,
_ when src.ParameterType.IsByRef => ParameterKind.Ref,
Expand Down Expand Up @@ -58,9 +57,9 @@ public static ParameterKind GetParameterKind(this ParameterInfo src)
//
// "IsReadOnlyAttribute" csak netstandard2.1-tol kezdve publikus.
//
src.GetCustomAttributes().Any(attr => attr.GetType().FullName == "System.Runtime.CompilerServices.IsReadOnlyAttribute")
src.GetCustomAttributes().Some(attr => attr.GetType().FullName == "System.Runtime.CompilerServices.IsReadOnlyAttribute")
#else
src.GetCustomAttribute<System.Runtime.CompilerServices.IsReadOnlyAttribute>() != null
src.GetCustomAttribute<System.Runtime.CompilerServices.IsReadOnlyAttribute>() is not null
#endif
;
}
Expand Down
117 changes: 67 additions & 50 deletions SRC/Private/Extensions/Metadata/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -50,16 +49,15 @@ public static string GetFriendlyName(this Type src)
if (src.IsGenericType)
src = src.GetGenericTypeDefinition();

return src.FullName;
return src.FullName?.TrimEnd('&');
}

public static Type? GetElementType(this Type src, bool recurse)
public static Type? GetInnermostElementType(this Type src)
{
Type? prev = null;

for (Type? current = src; (current = current.GetElementType()) != null;)
for (Type? current = src; (current = current!.GetElementType()) is not null;)
{
if (!recurse) return current;
prev = current;
}

Expand All @@ -85,10 +83,10 @@ public static string GetFriendlyName(this Type src)
if (gaCount is 0)
return enclosingType;

return enclosingType.MakeGenericType
(
src.GetGenericArguments().Take(gaCount).ToArray()
);
Type[] gas = new Type[gaCount];
Array.Copy(src.GetGenericArguments(), 0, gas, 0, gaCount);

return enclosingType.MakeGenericType(gas);
}

public static bool IsNested(this Type src) =>
Expand All @@ -97,7 +95,7 @@ public static string GetFriendlyName(this Type src)
// mar nem beagyazott tipus.
//

src.GetElementType(recurse: true)?.IsNested ?? src.IsNested;
src.GetInnermostElementType()?.IsNested ?? src.IsNested;

public static bool IsClass(this Type src) => !src.IsGenericParameter && src.IsClass;

Expand All @@ -106,7 +104,7 @@ public static string GetFriendlyName(this Type src)
public static IEnumerable<MethodInfo> ListMethods(this Type src, bool includeStatic = false) => src.ListMembersInternal
(
(t, f) => t.GetMethods(f),
m => m.GetAccessModifiers(),
MethodBaseExtensions.GetAccessModifiers,

//
// Metodus visszaterese lenyegtelen, csak a nev, parameter tipusa es atadasa, valamint a generikus argumentumok
Expand All @@ -117,18 +115,23 @@ public static string GetFriendlyName(this Type src)
{
HashCode hk = new();
foreach (var descr in m.GetParameters().Select(p => new { p.ParameterType, ParameterKind = p.GetParameterKind() }))
foreach (ParameterInfo p in m.GetParameters())
{
hk.Add(descr);
hk.Add(new
{
p.ParameterType,
ParameterKind = p.GetParameterKind()
});
}
return new
hk.Add(new
{
m.Name,
m.GetGenericArguments().Length,
m.IsStatic, // ugyanolyan nevvel es parameterekkel lehet statikus es nem statikus is
ParamzHash = hk.ToHashCode()
};
});
return hk.ToHashCode();
},
includeStatic
);
Expand All @@ -142,75 +145,86 @@ public static string GetFriendlyName(this Type src)
//

p => (AccessModifiers) Math.Max((int) (p.GetMethod?.GetAccessModifiers() ?? AccessModifiers.Unknown), (int) (p.SetMethod?.GetAccessModifiers() ?? AccessModifiers.Unknown)),
p => p.Name, // tipus lenyegtelen, tulajdonsagbol adott nevvel csak egy db lehet adott tipusban
p => new { p.Name, (p.GetMethod ?? p.SetMethod).IsStatic }.GetHashCode(),
includeStatic
);

public static IEnumerable<EventInfo> ListEvents(this Type src, bool includeStatic = false) => src.ListMembersInternal
(
(t, f) => t.GetEvents(f),
e => (e.AddMethod ?? e.RemoveMethod).GetAccessModifiers(),
e => e.Name, // tipus lenyegtelen, esemeny adott nevvel csak egy db lehet adott tipusban
e => new { e.Name, (e.AddMethod ?? e.RemoveMethod).IsStatic }.GetHashCode(),
includeStatic
);

private static IEnumerable<TMember> ListMembersInternal<TMember>(
this Type src,
Func<Type, BindingFlags, TMember[]> getter,
Func<TMember, AccessModifiers> getVisibility,
Func<TMember, object> getDescriptor,
bool includeStatic)
Func<TMember, int> getHashCode,
bool includeStatic) where TMember: MemberInfo
{
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

if (src.IsInterface)
//
// - A "BindingFlags.NonPublic" es "BindingFlags.FlattenHierarchy" nem
// ertelmezett interface-ekre.
// - Ez a megoldas a "new" kulcsszo altal elrejtett tagokat is visszaadja
// (ami szukseges interface-ek eseteben).
//

return src.GetInterfaces().Append(src).SelectMany(GetMembers);
{
foreach (Type t in src.GetHierarchy())
{
foreach (TMember member in getter(t, flags))
{
yield return member;
}
}
yield break;
}

//
// A BindingFlags.FlattenHierarchy csak a publikus es vedett tagokat adja vissza
// az os osztalyokbol, privatot nem, viszont az explicit implementaciok privat
// tagok...
// A BindingFlags.FlattenHierarchy csak a publikus es vedett tagokat adja vissza az os osztalyokbol,
// privatot nem, viszont az explicit implementaciok privat tagok...
//

//flags |= BindingFlags.FlattenHierarchy;
flags |= BindingFlags.NonPublic;
if (includeStatic) flags |= BindingFlags.Static;
if (includeStatic)
flags |= BindingFlags.Static;

var returnedMembers = new HashSet<object>();
HashSet<int> returnedMembers = new();

//
// Sorrend fontos, a leszarmazottol haladunk az os fele
//

return new[] { src }.Concat(src.GetBaseTypes()).SelectMany(GetMembers).Where
(
//
// Ha meg korabban nem volt visszaadva ("new", "override" miatt) es nem is privat akkor
// jok vagyunk.
//

m => getVisibility(m) is not AccessModifiers.Private && returnedMembers.Add
(
getDescriptor(m)
)
);
foreach (Type t in src.GetHierarchy())
{
foreach (TMember member in getter(t, flags))
{
//
// Ha meg korabban nem volt visszaadva ("new", "override" miatt) es nem is privat akkor
// jok vagyunk.
//

IEnumerable<TMember> GetMembers(Type t) => getter(t, flags);
if (getVisibility(member) is not AccessModifiers.Private && returnedMembers.Add(getHashCode(member)))
yield return member;
}
}
}

public static IEnumerable<Type> GetBaseTypes(this Type type)
{
for (Type? baseType = type.BaseType; baseType != null; baseType = baseType.BaseType)
for (Type? baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType)
yield return baseType;
}

public static IEnumerable<Type> GetHierarchy(this Type src)
{
yield return src;

foreach (Type t in src.IsInterface ? src.GetInterfaces() : src.GetBaseTypes())
{
yield return t;
}
}

public static IEnumerable<Type> GetOwnGenericArguments(this Type src)
{
if (!src.IsGenericType) yield break;
Expand All @@ -228,17 +242,20 @@ public static IEnumerable<Type> GetOwnGenericArguments(this Type src)
for(int i = 0; i < openArgs.Count; i++)
{
bool own = true;
for (Type? parent = src; (parent = parent.DeclaringType) != null;)
for (Type? parent = src; (parent = parent!.DeclaringType) is not null;)
{
//
// Ha "parent" nem generikus akkor a GetGenericArguments() ures tombot ad vissza
//

if (parent.GetGenericArguments().Contains(openArgs[i], ArgumentComparer.Instance))
if (parent.GetGenericArguments().Some(arg => ArgumentComparer.Instance.Equals(arg, openArgs[i])))
{
own = false;
break;
}
if (own) yield return closedArgs[i];
}
if (own)
yield return closedArgs[i];
}
}
}
Expand Down
Loading

0 comments on commit 7d0cb28

Please sign in to comment.