我有一个需要序列化的类MyClass
,一个属性将获得一个实现ICamera
的类列表,并创建一个在propertygrid中选择的类的实例
我是否可以使用所选实例序列化类。我想让它在没有MyClass
对类实现ICamera
以下是我目前为止的代码示例
[XmlRoot("MyClass")]
public class MyClass
{
private List<string> GetClassesImplementICameraList()
{
List<string> classObject = new List<string>();
var classesWithProperties =
Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.GetInterfaces().Contains(typeof(ICamera)) && t.IsClass)
.Select(c => new { ClassName = c.FullName})
.ToList();
foreach (var field in classesWithProperties)
{
classObject.Add(field.ClassName);
}
return classObject;
}
[Browsable(false)]
public List<string> CameraClassesList
{
get { return GetClassesImplementICameraList(); }
}
private ICamera _Camera = null;
[Category("Camera")]
[Description("The camera physically connected to the frame grabber.")]
public ICamera Camera
{
get { return _Camera as ICamera; }
}
private string _CameraMode = string.Empty;
[TypeConverter(typeof(CameraModeTypeConverter))]
[XmlIgnore]
[Category("Camera")]
public string CameraMode
{
get
{
return _CameraMode;
}
set
{
_CameraMode = value;
_Camera = GetCameraInstance(_CameraMode);
}
}
private ICamera GetCameraInstance(string name)
{
ICamera cameraInstance = null;
var instances = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetInterfaces().Contains(typeof(ICamera))
&& t.GetConstructor(Type.EmptyTypes) != null && t.FullName == name
select Activator.CreateInstance(t) as ICamera;
foreach (var instance in instances)
{
cameraInstance = instance;
}
return cameraInstance;
}
}