コード例 #1
0
 private static void OnValueMatching(object sender, ValueMatchingEventArgs e)
 {
     if (e.InputType == STRING_TYPE)
     {
         if (e.TargetType == DIRECTORYINFO_TYPE)
         {
             if (e.InputValue == null)
             {
                 e.SetValue(null);
             }
             else
             {
                 e.SetValue(new DirectoryInfo(Path.Combine(environment.WorkingDirectory.FullName, e.InputValue as string)));
             }
         }
         else if (e.TargetType == FILEINFO_TYPE)
         {
             if (e.InputValue == null)
             {
                 e.SetValue(null);
             }
             else
             {
                 e.SetValue(new FileInfo(Path.Combine(environment.WorkingDirectory.FullName, e.InputValue as string)));
             }
         }
     }
 }
コード例 #2
0
        public static bool TryMatchValue(Type targetType, Type type, object value, out object target)
        {
#if NETSTANDARD2_0 || NET452
            if (targetType.IsAssignableFrom(type))
            {
                target = value;
                return(true);
            }
#elif NETSTANDARD1_2
            if (targetType.GetTypeInfoFromCache().IsAssignableFrom(type.GetTypeInfoFromCache()))
            {
                target = value;
                return(true);
            }
#else
            // raise a compile error
            abcdefg
#endif

            else if (targetType.IsConvertible() && type.IsConvertible())
            {
                try
                {
                    target = Convert.ChangeType(value, targetType);
                    return(true);
                }
                catch
                {
                    target = null;
                    return(false);
                }
            }

            ValueMatchingEventArgs args = ObjectFactory.RaiseValueMatchingEvent(targetType, type, value);
            if (args != null && args.Handled)
            {
                target = args.Value;
                return(true);
            }
            target = null;
            return(false);
        }
コード例 #3
0
        private void OnValueMatching(object sender, ValueMatchingEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }

            if (e.TargetType.Name == "DirectoryInfo" && e.InputType.Name == "String")
            {
                string value = e.InputValue as string;
                if (value != null)
                {
                    e.SetValue(new DirectoryInfo(value));
                }
                else
                {
                    e.SetValue(null);
                }
            }
        }