查看此方法以了解如何创建所需的两个属性。
您需要以下属性和类型描述符类(取自 UrbanPotato 的代码)
// Source : taken from http://www.urbanpotato.net/default.aspx/document/2001 Seem to be down
// Allow the designer to load abstract forms
namespace YourNamespace
{
// Place this attribute on any abstract class where you want to declare
// a concrete version of that class at design time.
[AttributeUsage(AttributeTargets.Class)]
public class ConcreteClassAttribute : Attribute
{
Type _concreteType;
public ConcreteClassAttribute(Type concreteType)
{
_concreteType = concreteType;
}
public Type ConcreteType { get { return _concreteType; } }
}
// Here is our type description provider. This is the same provider
// as ConcreteClassProvider except that it uses the ConcreteClassAttribute
// to find the concrete class.
public class GeneralConcreteClassProvider : TypeDescriptionProvider
{
Type _abstractType;
Type _concreteType;
public GeneralConcreteClassProvider() : base(TypeDescriptor.GetProvider(typeof(Form))) { }
// This method locates the abstract and concrete
// types we should be returning.
private void EnsureTypes(Type objectType)
{
if (_abstractType == null)
{
Type searchType = objectType;
while (_abstractType == null && searchType != null && searchType != typeof(Object))
{
foreach (ConcreteClassAttribute cca in searchType.GetCustomAttributes(typeof(ConcreteClassAttribute), false))
{
_abstractType = searchType;
_concreteType = cca.ConcreteType;
break;
}
searchType = searchType.BaseType;
}
if (_abstractType == null)
{
// If this happens, it means that someone added
// this provider to a class but did not add
// a ConcreteTypeAttribute
throw new InvalidOperationException(string.Format("No ConcreteClassAttribute was found on {0} or any of its subtypes.", objectType));
}
}
}
// Tell anyone who reflects on us that the concrete form is the
// form to reflect against, not the abstract form. This way, the
// designer does not see an abstract class.
public override Type GetReflectionType(Type objectType, object instance)
{
EnsureTypes(objectType);
if (objectType == _abstractType)
{
return _concreteType;
}
return base.GetReflectionType(objectType, instance);
}
// If the designer tries to create an instance of AbstractForm, we override
// it here to create a concerete form instead.
public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
{
EnsureTypes(objectType);
if (objectType == _abstractType)
{
objectType = _concreteType;
}
return base.CreateInstance(provider, objectType, argTypes, args);
}
}
}
将它们分配给您的抽象形式,如下所示:
[TypeDescriptionProvider(typeof(GeneralConcreteClassProvider))]
[ConcreteClass(typeof(MyAbstractConcreteForm))]
public abstract partial class MyAbstractForm : Form
{
}
创建一个继承自抽象形式的新类。此类将由 Visual Studio 实例化
public class MyAbstractConcreteForm: MyAbstractForm
{
public MyAbstractConcreteForm() : base() { }
}
这应该有效。