我目前有这种类型的代码:
private void FillObject(Object MainObject, Foo Arg1, Bar Arg2)
{
if (MainObject is SomeClassType1)
{
SomeClassType1 HelpObject = (SomeClassType1)MainObject;
HelpObject.Property1 = Arg1;
HelpObject.Property2 = Arg2;
}
else if (MainObject is SomeClassType2)
{
SomeClassType2 HelpObject = (SomeClassType2)MainObject;
HelpObject.Property1 = Arg1;
HelpObject.Property2 = Arg2;
}
}
Run Code Online (Sandbox Code Playgroud)
假设SomeClassType1和SomeClassType2具有我想要分配的相同属性集(尽管它们可能在其他属性上有所不同),是否可以动态地将MainObject转换为适当的类型然后分配值,而不重复代码?这是我最后想看到的:
private void FillObject(Object MainObject, Foo Arg1, Bar Arg2)
{
Type DynamicType = null;
if (MainObject is SomeClassType1)
{
DynamicType = typeof(SomeClassType1);
}
else if (MainObject is SomeClassType2)
{
DynamicType = typeof(SomeClassType2);
}
DynamicType HelpObject = (DynamicType)MainObject;
HelpObject.Property1 = Arg1;
HelpObject.Property2 = Arg2;
}
Run Code Online (Sandbox Code Playgroud)
显然C#抱怨无法找到DynamicType:
找不到类型或命名空间名称'DynamicType'(您是否缺少using指令或程序集引用?)
这样的事情在C#2.0中是否可能?如果它比我现在的代码更混乱,那么我认为这样做没什么意义,但我很想知道.谢谢!
编辑:只是为了澄清,我完全理解实现一个接口是最合适的,也许是正确的解决方案.也就是说,我更感兴趣的是看看如何在不实现界面的情况下做到这一点.谢谢你的回复!
Joe*_*orn 15
看起来您关心的两种类型都实现了相同的两个属性.在这种情况下,您要做的是为这些属性定义一个接口:
public interface IMyInterface
{
public Foo Property1 {get; set;}
public Bar Property2 {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
然后,确保每个类告诉编译器他们实现了新接口.最后,使用一个泛型方法,该方法的类型参数受限于该interace:
private void FillObject<T>(T MainObject, Foo Arg1, Bar Arg2)
where T : IMyInterface
{
MainObject.Property1 = Arg1;
MainObject.Property2 = Arg2;
}
Run Code Online (Sandbox Code Playgroud)
请注意,即使使用额外的代码来声明界面,这些片段的结尾仍然比您在问题中发布的任何一个片段短,如果您关注的类型数量增加,则此代码更容易扩展.
基本上你在这里做的是根据对象的类型编写一个switch语句.除了你的第一个例子(最好是单调乏味)之外,没有其他好的方法可以做到这一点.
我写了一个用于切换类型的小框架,使语法更简洁.它允许您编写如下代码.
TypeSwitch.Do(
sender,
TypeSwitch.Case<Button>(
() => textBox1.Text = "Hit a Button"),
TypeSwitch.Case<CheckBox>(
x => textBox1.Text = "Checkbox is " + x.Checked),
TypeSwitch.Default(
() => textBox1.Text = "Not sure what is hovered over"));
Run Code Online (Sandbox Code Playgroud)
博客文章:http://blogs.msdn.com/jaredpar/archive/2008/05/16/switching-on-types.aspx
你能对SomeClassType1,SomeClassType2等进行更改吗?如果是,那么我建议您创建一个包含公共属性的接口,然后在该接口内FillObject()
进行类型转换以设置属性.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SO566510
{
interface IMyProperties
{
int Property1 { get; set; }
int Property2 { get; set; }
}
class SomeClassType1 : IMyProperties
{
public int Property1 { get; set; }
public int Property2 { get; set; }
}
class SomeClassType2 : IMyProperties
{
public int Property1 { get; set; }
public int Property2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
var obj1 = new SomeClassType1();
var obj2 = new SomeClassType2();
FillObject(obj1, 10, 20);
FillObject(obj2, 30, 40);
}
private static void FillObject(IMyProperties objWithMyProperties
, int arg1, int arg2)
{
objWithMyProperties.Property1 = arg1;
objWithMyProperties.Property2 = arg2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
36342 次 |
最近记录: |