Предположим, у меня есть следующий код:
Код: Выделить всё
// a class like this
class FirstObject {
public Object OneProperty {
get;
set;
}
// (other properties)
public Object OneMethod() {
// logic
}
}
// and another class with properties and methods names
// which are similar or exact the same if needed
class SecondObject {
public Object OneProperty {
get;
set;
}
// (other properties)
public Object OneMethod(String canHaveParameters) {
// logic
}
}
// the consuming code would be something like this
public static void main(String[] args) {
FirstObject myObject=new FirstObject();
// Use its properties and methods
Console.WriteLine("FirstObject.OneProperty value: "+myObject.OneProperty);
Console.WriteLine("FirstObject.OneMethod returned value: "+myObject.OneMethod());
// Now, for some reason, continue to use the
// same object but with another type
// -----> CHANGE FirstObject to SecondObject HERE
String.Format("String value: {0}", var1.Get(context)))
},
new Write() {
//Name = "WriteActivity2",
Text=new InArgument(
context =>
String.Format("Int value: {0}", var2.Get(context)))
}
}
};
Код: Выделить всё
Data var1=new Data("varname1", "string value");
Data var2=new Data("varname2", 123);
return new Sequence() {
DescriptiveName="Sequence Activity",
Data=new Collection { var1, var2 },
Activities=new Collection(){
new Write() {
DescriptiveName="WriteActivity1",
Text="String value: "+var1 //
Подробнее здесь: [url]https://stackoverflow.com/questions/5671146/change-object-type-at-runtime-maintaining-functionality[/url]
Мобильная версия