JJ.Framework.Reflection
1.5.6877.41324
Prefix Reserved
dotnet add package JJ.Framework.Reflection --version 1.5.6877.41324
NuGet\Install-Package JJ.Framework.Reflection -Version 1.5.6877.41324
<PackageReference Include="JJ.Framework.Reflection" Version="1.5.6877.41324" />
paket add JJ.Framework.Reflection --version 1.5.6877.41324
#r "nuget: JJ.Framework.Reflection, 1.5.6877.41324"
// Install JJ.Framework.Reflection as a Cake Addin #addin nuget:?package=JJ.Framework.Reflection&version=1.5.6877.41324 // Install JJ.Framework.Reflection as a Cake Tool #tool nuget:?package=JJ.Framework.Reflection&version=1.5.6877.41324
JJ.Framework.Reflection
Extensions to the System.Reflection
and System.Linq.Expressions
namespaces.
ExpressionHelper
Converts many types of lambda expressions into text or retrieves its resulting value. Here are some of the things it can do:
For instance:
ExpressionHelper.GetText(() => myParam.MyProperty.MyList[i].MySomething)
Will return the string:
"myParam.MyProperty.MyList[i].MySomething"
Similarly you can retrieve its value:
ExpressionHelper.GetValue(() => myParam.MyProperty.MyList[i].MySomething)
which can return:
3
It can also give you method info, parameter names and parameter value info from lambda expressions.
For instance:
ExpressionHelper.GetMethodCallInfo(() => MyMethod(3));
Will return:
MethodCallInfo
{
Name = "MyMethod",
Parameters =
{
ParameterType = typeof(int),
Name = "myParameter",
Value = 3
}
};
Accessor
Allows easy access to the internal, private or protected elements of an assembly or class.
For instance if you have the following private method in a class:
class MyClass
{
private int MyPrivateMethod(int myParameter)
{
return 3;
}
}
You can run that private method, that would otherwise not be available:
var myObject = new MyClass();
var accessor = new MyAccessor(myObject);
int myInt = accessor.MyPrivateMethod(1);
You can do that by writing the following wrapper class:
class MyAccessor
{
Accessor _accessor;
public MyAccessor(MyClass myObject) => _accessor = new Accessor(myObject);
public int MyPrivateMethod(int myParameter)
=> _accessor.InvokeMethod(() => MyPrivateMethod(default), myParameter);
}
ReflectionCache
Makes using reflection much faster in certain cases. For instance the GetProperties
method can be expensive, which is much faster through the ReflectionCache
class.
Example:
private static readonly ReflectionCache _reflectionCache
= new ReflectionCache(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] properties = _reflectionCache.GetProperties(typeof(MyClass));
You can also get other types of constructs in a fast way:
GetFields
GetConstructor
GetTypeByShortName
ReflectionExtensions
Various helper methods, but one of the most useful features is the GetImplementation
method and variations thereof, which allow you to retrieve implementations of a specified base class or interface from an assembly, which is useful for plug-in development.
GetBaseClasses
- Returns a type's base type and its base type, etc.
GetItemType
- Gets the item type of a collection type.
GetImplementations
- Allows you to retrieve implementations of a specified base class or interface from an assembly, which is useful for plug-in development.
GetPropertyOrException
Type.GetProperty
returns null if the property does not exist. This method is a little safer than that and throws a clear exception if the property does not exist.
GetUnderlyingNullableTypeFast
- Slightly faster than
Nullable.GetUnderlyingType
.
- Slightly faster than
GetValue
- Similar to
PropertyInfo.GetValue
, but for static properties you can now omit the object parameter.
- Similar to
IsAssignableFrom
/IsAssignableTo
- Similar to the original
Type.IsAssignableFrom
, but now also anIsAssignableTo
variation, if you find that more intuitive. Also has variations with a type argument e.g.myInterfaceType.IsAssignableFrom
<MyClass
>();
- Similar to the original
IsIndexer
- Can tell you if a
MethodBase
points to an indexer property.
- Can tell you if a
IsNullableType
- Can tell you if a
Type
is a nullable type.
- Can tell you if a
IsProperty
- Can tell you if a
MethodBase
is a property.
- Can tell you if a
IsReferenceType
- Can tell you if a
Type
is a reference type.
- Can tell you if a
IsStatic
- Can tell you if a
MemberInfo
is static.
- Can tell you if a
IsSimpleType
- Tells you if a
Type
is a 'simple type'. A simple type can be a .NET primitive types:Boolean
,Char
,Byte
,IntPtr
,UIntPtr
, the numeric types, their signed and unsigned variations, but alsoString
,Guid
,DateTime
,TimeSpan
andEnum
types.
- Tells you if a
ReflectionHelper
CreateInstance
- A variation on the existing
Type.CreateInstance
, that takes a type name as astring
, instead of aType
.
- A variation on the existing
IsDefault
- Can tell you if a value is the default value for its type. (E.g. 0 is the default value for Int32.)
IsSimpleType
- Tells you if a value is of a 'simple type'. A simple type can be a .NET primitive types:
Boolean
,Char
,Byte
,IntPtr
,UIntPtr
, the numeric types, their signed and unsigned variations, but alsoString
,Guid
,DateTime
,TimeSpan
andEnum
types.
- Tells you if a value is of a 'simple type'. A simple type can be a .NET primitive types:
TypesFromObjects
- You can pass objects to it, and it will return the concrete types of those objects, with some tolerance for nulls.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- JetBrains.Annotations (>= 2018.2.1)
- JJ.Framework.Common (>= 1.5.0 && < 1.6.0)
- JJ.Framework.PlatformCompatibility (>= 1.5.0 && < 1.6.0)
- JJ.Framework.Text (>= 1.5.0 && < 1.6.0)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on JJ.Framework.Reflection:
Package | Downloads |
---|---|
JJ.Framework.Exceptions
Contains many exception classes for common basic errors. Clear messages, concise syntax, strongly-typed, good performance. Generates messages like "myParent.MyChildren[0].MyProperty is null.", "height of 2 is less than 10.", "Customer with key { customerNumber = 1234, customerType = Subscriber } not found." |
|
JJ.Framework.Conversion
Makes it easier to convert simple types. |
|
JJ.Framework.Xml
XmlToObjectConverter converts an XmlDocument or string to an object graph. The way to map XML to classes is easier than the classic ways in .NET. XmlHelper allows you to access XML in a null-safe and multiplicity-safe way. It uses XmlDocument as the underlying .NET API. |
|
JJ.Framework.Collections
LINQ overloads. SelectRecursive SelectAncestors Add Remove AddRange Concat CrossJoin Distinct DistinctMany Except FirstWithClearException SingleOrDefaultWithClearException SingleWithClearException ForEach IndexOf TryGetIndexOf MinOrDefault MaxOrDefault PeekOrDefault PopOrDefault Product RemoveFirst Repeat ToHashSet ToNonUniqueDictionary TrimAll TryRemoveFirst Union Zip item.AsArray item.AsList item.AsEnumerable. Also a RedBlackTree and KeyValuePairHelper ConvertNamesAndValuesListToKeyValuePairs and ConvertNamesAndValuesListToDictionary. |
|
JJ.Framework.Testing
Helper for unit tests. Mimics the Assert class, but will display the tested expression in error messages, instead of being vague about it or laborious to program. It also offers methods to evaluate if the right exception goes off in the right spot with the right exception type and / or the right message. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.5.6877.41324 | 10,833 | 10/31/2018 |
1.4.6874.33783 | 8,712 | 10/27/2018 |
1.4.6870.35782 | 8,206 | 10/23/2018 |
1.4.6870.35459 | 9,227 | 10/23/2018 |
1.4.6862.40441 | 14,301 | 10/15/2018 |
1.3.6681.33420 | 7,327 | 4/17/2018 |
1.2.6640.39173 | 6,423 | 3/7/2018 |
1.1.0.31224 | 6,770 | 3/4/2018 |
1.0.6633.36153 | 1,746 | 2/28/2018 |
1.0.6633.34565 | 11,226 | 2/28/2018 |