ConverterBase

We found 10 examples in language CSharp for this search. You will see 39 fragments of code.

Other methods

        public void Register(ConverterBase fac)
        {
            if (fac == null) 
            {
                throw new ArgumentNullException("fac");
            }
            converters.Add(fac);
        }
        public void Unregister(ConverterBase fac)
        {
            if (fac == null)
            {
                throw new ArgumentNullException("fac");
            }
            converters.Remove(fac);
        }

        public ConverterBase[] GetAllConverters()
        {
            return converters.ToArray();
        }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceManager.Core;

namespace ResourceManager.Converter
{
    public abstract class ConverterBase
    { 
        public ConverterBase(VSSolution solution)
        {
            this.Solution = solution;
        }
        public ConverterBase(IEnumerable<VSProject> projects)
        {
            this.Projects = projects;
            this.Solution = projects.First().Solution;
        }
        public ConverterBase(VSProject project)
        {
            var list = new List<VSProject>(1);
            list.Add(project);
            this.Projects = list;
            this.Solution = project.Solution;
        }
        public ConverterBase(IEnumerable<IResourceFileGroup> fileGroups, VSSolution solution)
        {
            this.FileGroups = fileGroups;
            this.Solution = solution;
        }

        public IEnumerable<IResourceFileGroup> FileGroups
        {
            get;
            private set;
        }
        public IEnumerable<VSProject> Projects
        {
            get;
            private set;
        }
        public IEnumerable<VSCulture> Cultures
        {
            get;
            set;
        }
        public VSSolution Solution
        {
            get;
            private set;
        }
        public bool ExportDiff
        {
            get;
            set;
        }
        public bool ExportComments
        {
            get;
            set;
        }

	     public bool IncludeProjectsWithoutTranslations
	     { 
		      get; 
			   set;
	     }
         public bool AutoAdjustLayout
         {
             get;
             set;
         }
         public bool IgnoreInternalResources
         {
             get;
             set;
         }
    }
}


		#endregion

		#region "  CreateConverter  "

		private void CreateConverter(Type convType, object[] args)
		{
			if (typeof (ConverterBase).IsAssignableFrom(convType))
			{
				ConstructorInfo constructor;
				constructor = convType.GetConstructor(
                                        BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic,
                                        null, 
                                        ArgsToTypes(args),
                                        null);

				if (constructor == null)
				{
					if (args.Length == 0)
						throw new BadUsageException("Empty constructor for converter: " + convType.Name + " was not found. You must add a constructor without args (can be public or private)");
					else
						throw new BadUsageException("Constructor for converter: " + convType.Name + " with these arguments: (" + ArgsDesc(args) + ") was not found. You must add a constructor with this signature (can be public or private)");
				}

				try
				{
					Converter = (ConverterBase) constructor.Invoke(args);
				}
				catch (TargetInvocationException ex)
				{
					throw ex.InnerException;
				}

			}
#if ! MINI
			else if (convType.IsEnum)
			{
				Converter = new EnumConverter(convType);
			}
#endif			
			else
				throw new BadUsageException("The custom converter must inherit from ConverterBase");
		}

		#endregion

		#region "  ArgsToTypes  "

		private static Type[] ArgsToTypes(object[] args)
		{
			if (args == null)
				throw new BadUsageException("The args to the constructor can be null, if you do not want to pass anything into them.");

			var res = new Type[args.Length];

			for (int i = 0; i < args.Length; i++)
			{
				if (args[i] == null)
					res[i] = typeof (object);
				else
					res[i] = args[i].GetType();
			}

			return res;

		}

		private static string ArgsDesc(object[] args)
		{
			
			string res = DisplayType(args[0]);

			for(int i = 1; i < args.Length; i++)
				res += ", " + DisplayType(args[i]);

			return res;
		}

		private static string DisplayType(object o)
		{
			if (o == null)
				return "Object";
			else
				return o.GetType().Name;
		}

		#endregion


        internal void ValidateTypes(FieldInfo fi)
        {
            bool valid = false;

            Type fieldType = fi.FieldType;

            if (fieldType.IsValueType &&
                  fieldType.IsGenericType &&
                    fieldType.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                fieldType = fieldType.GetGenericArguments()[0];
            }

            switch (Kind)
            {
                case ConverterKind.None:
                    valid = true;
                    break;

                case ConverterKind.Date:
                case ConverterKind.DateMultiFormat:
                    valid = typeof(DateTime) == fieldType;
                    break;

                case ConverterKind.Byte:
                case ConverterKind.SByte:
                case ConverterKind.Int16:
                case ConverterKind.Int32:
                case ConverterKind.Int64:
                case ConverterKind.UInt16:
                case ConverterKind.UInt32:
                case ConverterKind.UInt64:
                case ConverterKind.Decimal:
                case ConverterKind.Double:
                case ConverterKind.Single:
                case ConverterKind.Boolean:
                case ConverterKind.Char:
                case ConverterKind.Guid:
                    valid = Kind.ToString() == fieldType.UnderlyingSystemType.Name;
                    break;
                case ConverterKind.PercentDouble:
                    valid = typeof(double) == fieldType;
                    break;

            }

            if (valid == false)
               throw new BadUsageException(
                   "The converter of the field: '" + fi.Name + "' is wrong. The field is of Type: " + fieldType.Name + " and the converter is for type: " + Kind.ToString());
        }

        public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value?.ToString();
        }

        public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value?.ToString();
        }


        public virtual void Convert(BaseData baseData)
        {
        }


        /// <summary>
        /// Is invoked by a parser or another producer whenever an
        /// array begins. This method must return an array consumer which
        /// is used to pass the array elements. Once, there are no more 
        /// elements, the <paramref name="whenDone"/> callback must be called 
        /// by the <see cref="IJsonArrayConsumer"/> implementation in order to pass the
        /// resulting .NET object.
        /// 
        /// This implementation in <see cref="ConverterBase"/> throws an exception. If a
        /// deriving converter accepts JSON arrays, this method must be overridden.
        /// </summary>
        /// <param name="whenDone">A callback which must be used to pass
        /// the resulting .NET object, when Done() of the array consumer
        /// gets called.</param>
        /// <returns>An <see cref="IJsonArrayConsumer"/> implementation which
        /// is used to pass the elements of the array.</returns>
        public virtual IJsonArrayConsumer FromArray(Action<object> whenDone)
        {
            throw new Exception($"Cannot convert JSON array to type '{Type}'.");
        }

        /// <summary>
        /// Is invoked by a parser or another producer whenever a boolean
        /// value was parsed/produced.
        /// 
        /// This implementation in <see cref="ConverterBase"/> throws an exception. If a
        /// deriving converter accepts JSON booleans, this method must be overridden.
        /// </summary>
        /// <param name="value">The boolean value.</param>
        /// <returns>The resulting .NET object.</returns>
        public virtual object FromBoolean(bool value)
        {
            throw new Exception($"Cannot convert JSON boolean to type '{Type}'.");
        }

        /// <summary>
        /// Is invoked by a parser or another producer whenever a 'null'
        /// value was parsed/produced.
        /// 
        /// This implementation in <see cref="ConverterBase"/> throws an exception. If a
        /// deriving converter accepts JSON null values, this method must be overridden.
        /// </summary>
        /// <returns>The resulting .NET object.</returns>
        public virtual object FromNull()
        {
            throw new Exception($"Cannot convert JSON null to type '{Type}'.");
        }

        /// <summary>
        /// Is invoked by a parser or another producer whenever a floating
        /// point value was parsed/produced.
        /// 
        /// This implementation in <see cref="ConverterBase"/> throws an exception. If a
        /// deriving converter accepts JSON floating point numbers, this method must be overridden.
        /// </summary>
        /// <param name="value">The floating point value.</param>
        /// <returns>The resulting .NET object.</returns>
        public virtual object FromNumber(double value)
        {
            throw new Exception($"Cannot convert JSON floating point number to type '{Type}'.");
        }

        /// <summary>
        /// Is invoked by a parser or another producer whenever an integer
        /// value was parsed/produced.
        /// 
        /// This implementation in <see cref="ConverterBase"/> throws an exception. If a
        /// deriving converter accepts JSON integer numbers, this method must be overridden.
        /// </summary>
        /// <param name="value">The integer value.</param>
        /// <returns>The resulting .NET object.</returns>
        public virtual object FromNumber(long value)
        {
            throw new Exception($"Cannot convert JSON integer number to type '{Type}'.");
        }

        /// <summary>
        /// Is invoked by a parser or another producer whenever an unsigned integer
        /// value was parsed/produced.
        /// 
        /// This implementation in <see cref="ConverterBase"/> casts the unsigned
        /// integer to a signed integer and passes it to the other FromNumber overload.
        /// </summary>
        /// <param name="value">The unsigned integer value.</param>
        /// <returns>The resulting .NET object.</returns>
        public virtual object FromNumber(ulong value)
        {
            return FromNumber((long)value);
        }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceManager.Core;

namespace ResourceManager.Converter
{
    public abstract class ConverterBase
    { 
        protected ConverterBase(VSSolution solution)
        {
            this.Solution = solution;
        }
        protected ConverterBase(IEnumerable<VSProject> projects)
        {
            this.Projects = projects;
            this.Solution = projects.First().Solution;
        }
        protected ConverterBase(VSProject project)
        {
            if (project == null)
                throw new ArgumentNullException("project");

            var list = new List<VSProject>(1);
            list.Add(project);
            this.Projects = list;
            this.Solution = project.Solution;
        }
        protected ConverterBase(IEnumerable<IResourceFileGroup> fileGroups, VSSolution solution)
        {
            this.FileGroups = fileGroups;
            this.Solution = solution;
        }

        public IEnumerable<IResourceFileGroup> FileGroups
        {
            get;
            private set;
        }
        public IEnumerable<VSProject> Projects
        {
            get;
            private set;
        }
        public IEnumerable<VSCulture> Cultures
        {
            get;
            set;
        }
        public VSSolution Solution
        {
            get;
            private set;
        }
        public bool ExportDiff
        {
            get;
            set;
        }
        public bool ExportComments
        {
            get;
            set;
        }

	     public bool IncludeProjectsWithoutTranslations
	     { 
		      get; 
			   set;
	     }
         public bool AutoAdjustLayout
         {
             get;
             set;
         }
         public bool IgnoreInternalResources
         {
             get;
             set;
         }
    }
}


        [ExcludeFromCodeCoverage]
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return string.Empty;
            bool didParse = int.TryParse(value.ToString(), out var nValue);
            if (!didParse)
                nValue = 0;
            return string.Format(CultureInfo.CurrentCulture, "{0}${1}", (nValue < 0) ? Strings.LostString : Strings.WonString, Math.Abs(nValue));
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return string.Empty;
            bool didGet = suitMap.TryGetValue((CardColour)value, out var imageUri);
            return didGet ? imageUri : string.Empty;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return string.Empty;
            bool didGet = numberMap.TryGetValue((CardValue)value, out var retVal);
            return didGet ? retVal : string.Empty;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Colors.Black;
            bool didGet = colourMap.TryGetValue((CardColour)value, out var retColour);
            return didGet ? retColour : Colors.Black;
        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            /* First object: selected index (int)
             * Second object: button index (int) */
            if (values == null)
                return whiteColour;
            var castValues = values.Cast<int>();
            if (castValues.Count() < 2)
                return whiteColour;
            return (castValues.ElementAt(0) == castValues.ElementAt(1)) ? blackColour : whiteColour;
        }


    public object FromJsonSerializable(JToken js)
    {
        return parser(js);
    }

    public JToken ToJsonSerializable(object obj)
    {
        return serializer(obj);
    }

    public T FromJsonSerializable(JToken js)
    {
        return parser(js);
    }

    public JToken ToJsonSerializable(T obj)
    {
        return serializer(obj);
    }

Microsoft.Internal.VisualStudio.PlatformUI.ConverterBase : IVsUIDataConverter

Methods :

public abstract IVsUIObject Convert(IVsUIObject source = )
public abstract VSUIDATAFORMAT get_SourceFormat()
public abstract VSUIDATAFORMAT get_TargetFormat()
public abstract String get_Type()
public Int32 Convert(IVsUIObject source = , IVsUIObject& converted = )
public Int32 get_ConvertibleFormats(UInt32& source = , UInt32& target = )
public Int32 get_Type(String& type = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()