public void TestGetAllPublicStaticProperties()
        {
            var staticProps = ReflectionUtility.GetAllPublicStaticProperties(new MockClass().GetType());

            Assert.IsNotNull(staticProps, "not null");
            Assert.IsTrue(staticProps.Length > 0, "non-empty properties array");

            Assert.IsTrue(staticProps.Length < ReflectionUtility.GetAllDataFields(new MockClass().GetType()).Length, "static properties are subset of all properties");
        }
        public void TestGetAllPublicInstanceProperties()
        {
            var dataFields = ReflectionUtility.GetAllPublicInstanceProperties(new MockClass().GetType());

            Assert.IsNotNull(dataFields, "not null");
            Assert.IsTrue(dataFields.Length > 0, "non-empty data fields array");

            Assert.IsTrue(dataFields.Length < ReflectionUtility.GetAllDataFields(new MockClass().GetType()).Length, "instance fields are subset of all fields");
        }
        public void TestGetAllDataFields()
        {
            var dataFields = ReflectionUtility.GetAllDataFields(new MockClass().GetType());

            Assert.IsNotNull(dataFields, "not null");
            Assert.IsTrue(dataFields.Length > 0, "non-empty data fields array");

            var instanceFields = ReflectionUtility.GetAllPublicInstanceProperties(new MockClass().GetType());
            var staticFields   = ReflectionUtility.GetAllPublicStaticProperties(new MockClass().GetType());

            Assert.AreEqual(dataFields.Length, instanceFields.Length + staticFields.Length, "all fields = instance fields + static fields");
        }
Exemplo n.º 4
0
        public static IDictionary <string, object?>?CaptureState(
            object instance,
            string?instanceName,
            IDictionary <string, object?>?stateCapture
            )
        {
            if (instance == null)
            {
                return(null);
            }

            Type instanceType = instance.GetType();

            if (instanceType == null)
            {
                return(null);
            }
            if (instanceType.IsInterface || instanceType.IsEnum)
            {
                return(null);
            }
            if (instanceType == typeof(Type) && instanceType.IsAbstract && instanceType.IsSealed)
            {
                return(RollbarAssistant.CaptureState((Type)instance));
            }

            string dataMemberNamePrefix = $"{instanceName ?? string.Empty}[{instanceType.FullName}].";
            var    memberInfos          = ReflectionUtility.GetAllDataFields(instanceType);

            if (stateCapture == null)
            {
                stateCapture = new Dictionary <string, object?>(memberInfos.Length);
            }
            foreach (var memberInfo in memberInfos)
            {
                object?value = memberInfo.GetValue(instance);
                if (value != null && value.GetType().IsEnum)
                {
                    value = value.ToString();
                }
                stateCapture[dataMemberNamePrefix + memberInfo.Name] = value;
            }

            return(stateCapture);
        }
Exemplo n.º 5
0
        public static IDictionary <string, object?>?CaptureState(
            Type staticType,
            IDictionary <string, object?>?stateCapture
            )
        {
            if (staticType.IsInterface || staticType.IsEnum)
            {
                return(null);
            }

            string dataMemberNamePrefix = $"[{staticType.FullName}].";
            var    memberInfos          = ReflectionUtility.GetAllDataFields(staticType);

            if (stateCapture == null)
            {
                stateCapture = new Dictionary <string, object?>(memberInfos.Length);
            }
            foreach (var memberInfo in memberInfos)
            {
                stateCapture[dataMemberNamePrefix + memberInfo.Name] = memberInfo.GetValue(null);
            }

            return(stateCapture);
        }