Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If Property Value Is Null GetToStringValue will get System.NullReferenceException #1

Closed
shps951023 opened this issue Mar 24, 2019 · 1 comment
Labels
bug

Comments

@shps951023
Copy link
Owner

@shps951023 shps951023 commented Mar 24, 2019

        [TestMethod]
        public void PropertyValueIsNull()
        {
            MyClass data = new MyClass { MyProperty2=null} ;
            var props = typeof(MyClass).GetProperties();
            foreach (var prop in props)
            {
                var result = prop.GetToStringValue(data); //System.NullReferenceException: 'Object reference not set to an instance of an object.'
            }
        }
@shps951023 shps951023 added the bug label Mar 24, 2019
@shps951023

This comment has been minimized.

Copy link
Owner Author

@shps951023 shps951023 commented Mar 24, 2019

V1.0.1 fix bug

code:

        /// <summary>
        /// Compiler Method Like:
        /// <code>
        ///     string GetterToStringFunction(object i) => GetterFunction(i).ToString() ; 
        ///     object GetterFunction(object i) => (i as MyClass).MyProperty1 as object ;
        /// </code>
        /// </summary>
        public static string GetToStringValue<T>(this PropertyInfo propertyInfo, T instance)
            => instance != null ? ValueGetterCache<T, object>.GetOrAddFunctionCache(propertyInfo)(instance)?.ToString() : null;
@shps951023 shps951023 closed this Mar 24, 2019
shps951023 added a commit that referenced this issue Mar 24, 2019
- [X] Fix [If Property Value Is Null GetToStringValue will get System.NullReferenceException · Issue #1 ](#1)
@shps951023 shps951023 mentioned this issue Mar 24, 2019
1 of 1 task complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.