Example #1
0
 public void CreateInstanceAutoFillGenericParameters_GenericTypeWithBaseClassConstraintPassed_ExceptionThrown()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         ActivatorUtils.CreateInstanceAutoFillGenericParameters(typeof(GenericClassWithBaseClassConstraint <>));
     });
 }
Example #2
0
        /// <summary>
        /// Gets text to be used as a member value
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <returns>The text to be used as a member value. Null if the member has no value or value cannot be determined.</returns>
        public string GetMemberValueText(MemberInfo memberInfo)
        {
            if (memberInfo.DeclaringType == null)
            {
                return(null);
            }

            try
            {
                object instance      = memberInfo.IsStatic() ? null : ActivatorUtils.CreateInstanceAutoFillGenericParameters(memberInfo.DeclaringType);
                var    valueObj      = new object();
                object valueObjGuard = valueObj;

                switch (memberInfo)
                {
                case FieldInfo fieldInfo:
                    valueObj = fieldInfo.GetValue(instance);
                    break;

                case PropertyInfo propertyInfo:
                    valueObj = propertyInfo.GetValue(instance);
                    break;
                }

                // if valueObj hasn't been assigned in the switch
                if (valueObj == valueObjGuard)
                {
                    return(null);
                }

                // if valueObj's value is the default value for its type
                if (valueObj == null || valueObj.Equals(TypeUtils.GetDefaultValue(valueObj.GetType())))
                {
                    return(null);
                }

                string memberType = _typeService.GetTsTypeName(memberInfo).GetTsTypeUnion(0);
                string quote      = GeneratorOptions.SingleQuotes ? "'" : "\"";

                switch (valueObj)
                {
                case Guid valueGuid when memberType == "string":
                    return(quote + valueGuid + quote);

                case DateTime valueDateTime when memberType == "Date":
                    return($@"new Date({quote}{valueDateTime}{quote})");

                case DateTime valueDateTime when memberType == "string":
                    return(quote + valueDateTime + quote);

                case DateTimeOffset valueDateTimeOffset when memberType == "Date":
                    return($@"new Date({quote}{valueDateTimeOffset}{quote})");

                case DateTimeOffset valueDateTimeOffset when memberType == "string":
                    return(quote + valueDateTimeOffset + quote);

                default:
                    return(JsonConvert.SerializeObject(valueObj).Replace("\"", quote));
                }
            }
            catch (MissingMethodException e)
            {
                _logger?.Log($"Cannot determine the default value for member '{memberInfo.DeclaringType.FullName}.{memberInfo.Name}', because type '{memberInfo.DeclaringType.FullName}' has no default constructor.", LogLevel.Debug);
            }
            catch (ArgumentException e) when(e.InnerException is TypeLoadException)
            {
                _logger?.Log($"Cannot determine the default value for member '{memberInfo.DeclaringType.FullName}.{memberInfo.Name}', because type '{memberInfo.DeclaringType.FullName}' has generic parameters with base class or interface constraints.", LogLevel.Debug);
            }
            catch (Exception e)
            {
                _logger?.Log($"Cannot determine the default value for member '{memberInfo.DeclaringType.FullName}.{memberInfo.Name}', because an unknown exception occurred: '{e.Message}'", LogLevel.Debug);
            }

            return(null);
        }
Example #3
0
        public void CreateInstanceAutoFillGenericParameters_GenericTypeWithDefaultConstructorConstraintPassed_InstanceCreated()
        {
            object instance = ActivatorUtils.CreateInstanceAutoFillGenericParameters(typeof(GenericClassWithDefaultConstructorConstraint <>));

            Assert.True(instance is GenericClassWithDefaultConstructorConstraint <object>);
        }
Example #4
0
        public void CreateInstanceAutoFillGenericParameters_GenericTypeWithClassAndStructConstraintsPassed_InstanceCreated()
        {
            object instance = ActivatorUtils.CreateInstanceAutoFillGenericParameters(typeof(GenericClassWithClassAndStructConstraints <, ,>));

            Assert.True(instance is GenericClassWithClassAndStructConstraints <object, int, object>);
        }
Example #5
0
        public void CreateInstanceAutoFillGenericParameters_NonGenericTypePassed_InstanceCreated()
        {
            object instance = ActivatorUtils.CreateInstanceAutoFillGenericParameters(typeof(NonGenericClass));

            Assert.True(instance is NonGenericClass);
        }