Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
markellus committed Feb 23, 2020
0 parents commit 9b49eb4
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The MIT License (MIT)

Copyright © 2020 Marcel Bulla <njage@marcel-bulla.de>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# NJAGE .Net Core Internals

**Work In Progress**

## NJAGE Dependencies:

none
16 changes: 16 additions & 0 deletions src/De.Markellus.Njage.NetInternals.projitems
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>8b8e670e-51f0-45fe-9a1c-227a99f0f58f</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>De.Markellus.Njage.NetInternals</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)njGenericRuntimeCaller.cs" />
<Compile Include="$(MSBuildThisFileDirectory)njReflectiveEnumerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)njRuntimeVerifier.cs" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions src/De.Markellus.Njage.NetInternals.shproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>8b8e670e-51f0-45fe-9a1c-227a99f0f58f</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="De.Markellus.Njage.NetInternals.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
59 changes: 59 additions & 0 deletions src/njGenericRuntimeCaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Reflection;
using De.Markellus.Njage.NetInternals;

namespace Markellus.Njage.NetInternals
{
public static class njGenericRuntimeCaller
{
static njGenericRuntimeCaller()
{
njRuntimeVerifier.Verify();
}

/// <summary>
/// Dynamically compiles and calls a generic function if only a type object is available at runtime.
/// </summary>
/// <param name="tClass">The type of the class that contains the generic method.</param>
/// <param name="instance">Object instance on which the method is applied, or null if it is a static function. The object must be of type <see cref="typeClass"/>.</param>
/// <param name="strMethod">The name of the generic method to be recompiled.</param>
/// <param name="bindingFlags">Method Flags</param>
/// <param name="typeTarget">The type of the generic function, usually enclosed in triangular brackets (<>).</param>
/// <param name="arguments">Arguments of the generic function</param>
/// <returns>The return value of the function, if available.</returns>
public static object Invoke(Type tClass, object instance, string strMethod, BindingFlags bindingFlags,
Type typeTarget, params object[] arguments)
{
MethodInfo method = tClass.GetMethod(strMethod, bindingFlags);
method = method?.MakeGenericMethod(typeTarget);
return method?.Invoke(instance, arguments);
}

/// <summary>
/// Dynamically compiles a generic type of this base type.
/// </summary>
/// <param name="tGenericArg">The generic argument</param>
/// <param name="arrConstructorArgs">Arguments of the constructor</param>
/// <typeparam name="T">The type of the class</typeparam>
/// <returns>A new instance of <see cref="tTarget"/> with the specified generic argument.</returns>
public static T Invoke<T>(Type tGenericArg, params object[] arrConstructorArgs)
{
return (T) Invoke(typeof(T), tGenericArg, arrConstructorArgs);
}

/// <summary>
/// Dynamically compiles a generic type if only a type object is available at runtime.
/// </summary>
/// <param name="tTarget">The type of the class</param>
/// <param name="tGenericArg">The generic argument</param>
/// <param name="arrConstructorArgs">Arguments of the constructor</param>
/// <returns>A new instance of <see cref="tTarget"/> with the specified generic argument.</returns>
public static object Invoke(Type tTarget, Type tGenericArg, params object[] arrConstructorArgs)
{
Type typeGeneric = tTarget.MakeGenericType(tGenericArg);

return Activator.CreateInstance(typeGeneric,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, arrConstructorArgs, null);
}
}
}
118 changes: 118 additions & 0 deletions src/njReflectiveEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/***********************************************************/
/* NJAGE Engine - .NET Core Internals */
/* */
/* Copyright 2020 Marcel Bulla. All rights reserved. */
/* Licensed under the MIT License. See LICENSE in the */
/* project root for license information. */
/***********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace De.Markellus.Njage.NetInternals
{
public static class njReflectiveEnumerator
{
static njReflectiveEnumerator()
{
njRuntimeVerifier.Verify();
}

/// <summary>
/// Returns an instance of each class of all loaded assemblies with a specified base type.
/// </summary>
/// <param name="constructorArgs">Constructor arguments for all new instances</param>
/// <typeparam name="T">The base type of all classes</typeparam>
public static IEnumerable<T> GetInstancesOfType<T>(params object[] constructorArgs) where T : class
{
Assembly[] arrAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in arrAssemblies)
{
foreach (T t in GetInstancesOfType<T>(assembly, constructorArgs))
{
yield return t;
}
}
}

/// <summary>
/// Returns an instance of each class with a specified base type.
/// </summary>
/// <param name="assembly">The assembly which contains the target classes</param>
/// <param name="constructorArgs">Constructor arguments for all new instances</param>
/// <typeparam name="T">The base type of all classes</typeparam>
public static IEnumerable<T> GetInstancesOfType<T>(Assembly assembly, params object[] constructorArgs) where T : class
{
try
{
//.NET Core 3.x Bug: Unit tests can load assemblies that are invalid.
//Such an assembly will throw an exception when meta information of any type is accessed.
var info = assembly.DefinedTypes;
}
catch
{
return new List<T>(0);
}

return assembly.GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(T)))
.Select(t => (T)Activator.CreateInstance(t,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, constructorArgs, null));
}

/// <summary>
/// Tries to find a type object that matches the given name.
/// </summary>
/// <param name="strType">The type as a string</param>
/// <returns>A type object or null if there is no matching type.</returns>
public static Type GetTypeFromString(string strType)
{
Assembly[] arrAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in arrAssemblies)
{
try
{
//.NET Core 3.x Bug: Unit tests can load assemblies that are invalid.
//Such an assembly will throw an exception when meta information of any type is accessed.
var info = assembly.DefinedTypes;
}
catch
{
continue;
}

foreach (Type type in assembly.GetTypes())
{
if (type.FullName == strType)
{
return type;
}
}
}

return null;
}

/*// <summary>
/// Returns an instance of each class of all loaded assemblies which are derived from a specified interface.
/// </summary>
/// <param name="assembly"></param>
/// <param name="constructorArgs">Constructor arguments for all new instances</param>
/// <typeparam name="T">The interface of all classes</typeparam>
public static T GetInstanceOfInterface<T>(Assembly assembly, params object[] constructorArgs)
{
Type type = assembly.GetTypes()
.FirstOrDefault(t => t.IsClass && !t.IsAbstract && typeof(T).IsAssignableFrom(t));
if (type == null)
{
return default;
}
return (T) Activator.CreateInstance(type,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
null, constructorArgs, null);
}*/
}
}
34 changes: 34 additions & 0 deletions src/njRuntimeVerifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/***********************************************************/
/* NJAGE Engine - .NET Core Internals */
/* */
/* Copyright 2020 Marcel Bulla. All rights reserved. */
/* Licensed under the MIT License. See LICENSE in the */
/* project root for license information. */
/***********************************************************/

using System;

namespace De.Markellus.Njage.NetInternals
{
public static class njRuntimeVerifier
{
/// <summary>
/// Verifies the runtime.
/// </summary>
public static void Verify()
{
if(!Is64BitConfiguration())
{
Environment.Exit(int.MinValue);
}
}

/// <summary>
/// Returns true if the runtime is 64 Bit.
/// </summary>
public static bool Is64BitConfiguration()
{
return IntPtr.Size == 8;
}
}
}

0 comments on commit 9b49eb4

Please sign in to comment.