|
using System;
using System.Globalization;
using System.Reflection;
using System.Data;
using System.Data.Objects;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace EFWinforms.Design
{
/// <summary>
/// TypeConverter that shows a list of types that inherit from ObjectContext and
/// can be assigned to the ObjectContextType property.
/// </summary>
public class ObjectContextTypeTypeConverter : ReferenceConverter
{
//----------------------------------------------------------------------------
#region ** ctor
public ObjectContextTypeTypeConverter()
: base(typeof(Type))
{
}
#endregion
//----------------------------------------------------------------------------
#region ** overrides
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
List<Type> values = new List<Type>();
// value for no data source
values.Add(null);
// all types that derive from ObjectContext
foreach (Type t in GetObjectContextTypes(context))
{
values.Add(t);
}
// done
return new StandardValuesCollection(values);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
var values = GetObjectContextTypes(context);
foreach (var t in values)
{
if (t.ToString() == (string)value)
{
return t;
}
}
return null;
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is Type)
{
return value.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
List<Type> GetObjectContextTypes(ITypeDescriptorContext context)
{
var values = new List<Type>();
var tds = context.GetService(typeof(ITypeDiscoveryService)) as ITypeDiscoveryService;
if (tds != null)
{
foreach (Type t in tds.GetTypes(typeof(ObjectContext), true))
{
if (t.IsPublic && t.IsVisible && !t.IsAbstract)
{
values.Add(t);
}
}
}
return values;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Software Architect/Developer with several years experience creating and delivering software.
Full-stack Web development (including React, Firebase, TypeScript, HTML, CSS), Entity Framework, C#, MS SQL Server.
Passionate about new technologies and always keen to learn new things as well as improve on existing skills.