Code Monkey home page Code Monkey logo

jcd.reflection's Introduction

Jcd.Reflection

A dotnetstandard2.0 library containing some commonly implemented reflection helpers.

v2.0 has many breaking changes. See the Version History for details.

Examples

    internal static class Program
    {
        private static void Main()
        {
            var c = new TestClass();
            // set the private field, _field
            c.SetValue("_field",10);

            // now get its value.
            var val = (int)c.GetValue("_field");
            Console.WriteLine(val);

            // Now set a private property with a backing field.
            c.SetValue("PrivateProperty",20);

            // Now call a private helper method that returns the value from the backing field.
            val = c.Invoke<int>("InternalGetField");
            Console.WriteLine(val);

Special Thanks To

Scrutor inspired the technique I'm using for finding implementations of specific types as well as a couple of other handy utility methods.

Badges

GitHub Build status CodeFactor Grade

MyGet Nuget

API Docs

Version History

v2.0 Breaking Changes

Version 2.0 comes with a significant number of breaking changes for advanced use cases. These are listed below

  • ExpandoObjectExtensions was removed. It wasn't functioning as planned. An overhaul of the approach is needed.
  • All overloads of FilterMethods were renamed to GetMethods
  • The filter parameter was moved to the last parameter on an overload of GetMethods (formerly FilterMethods).
  • Renamed EnumerationSettings to Filter in MemberInfoEnumerator, FieldOrPropertyEnumerator, and MethodInfoEnumerator
  • Moved and renamed type from MethodInfoEnumerator.Settings to MethodInfoFilter.
  • Moved predefined instances of MethodInfoEnumerator.Settings from MethodInfoEnumerator to MethodInfoFilter.
  • Moved and renamed type from MemberInfoEnumerator.Settings to MemberInfoFilter.
  • Moved and renamed type from FieldOrPropertyEnumerator.Settings to FieldOrPropertyInfoFilter.
  • MethodInfoFilter, MemberInfoFilter, FieldOrPropertyInfoFilter fields are now get-only properties, requiring a call to new to create new instances.
  • To facilitate the use of init properties on MethodInfoFilter, MemberInfoFilter, FieldOrPropertyInfoFilter types, a custom IsExternalInit is required of target frameworks not supporting it. See this project's source code for an example (IsExternalInit.cs).
  • Extension methods GetCusomAttributes and HasAttribute were moved into classes named after the underlying method. This will only break non-extension invocations.

jcd.reflection's People

Contributors

jason-c-daniels avatar

Watchers

 avatar

jcd.reflection's Issues

ToPropertyInfoValuePairs doesn't use the skip parameter

        public static IEnumerable<KeyValuePair<PropertyInfo, object>> 
            ToPropertyInfoValuePairs(this IEnumerable<PropertyInfo> items, 
                                     object item, Func<PropertyInfo, bool> skip = null)

Doesn't use the skip predicate. This makes it so the user must use LINQ to filter. Either implement it or remove it.

ToExpandoObject doesn't use the visited parameter

        public static ExpandoObject ToExpandoObject(this object self, 
                                                      HashSet<object> visited = null, 
                                                      Func<string,string> keyRenamingStrategy=null, 
                                                      Func<string, object, bool> valueRetentionStrategy = null)

doesn't use the visited parameter. Lacking this makes it difficult to provide a set of objects to skip processing on, known before the attempted conversion.

Also the unit tests never checked for this usage.

Acceptance criteria:

  1. Visited is used to prepopulate a set of child nodes to skip in the object graph
  2. The unit tests check that this is indeed being processed.

ToDictionaryTree doesn't use "visited" parameter

public static IDictionary<string, object> ToDictionaryTree(this object self,
                                                               HashSet<object> visited = null,
                                                               Func<string, string> keyRenamingStrategy = null,
                                                               Func<string, object, bool> valueRetentionStrategy = null)

doesn't use the visited parameter. Lacking this makes it difficult to provide a set of objects to skip processing on, known before the attempted conversion.

Also the unit tests never checked for this usage.

Acceptance criteria:

  1. Visited is used to prepopulate a set of child nodes to skip in the object graph
  2. The unit tests check that this is indeed being processed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.