我需要确定我的代码在某个特定点上使用哪个对象,并只写出特定于该类的属性。我无法弄清楚该怎么做。我被告知我可以做到,但我无法理解。有人可以告诉我如何确定我正在使用哪个对象并只编写特定于该类的属性?
我已经查看了其他问题,但我还不够聪明,不能让它符合我的榜样。
下面,我重新创建了一个我正在使用的代码的示例。我可以看到所有代码,但我只允许在一个方法中工作(这个例子叫做" MethodIAmWorkingIn")。只允许我在该方法中进行修改。
public class Program
{
static void Main(string[] args)
{
TestDetailsAndResultsContainer container = new TestDetailsAndResultsContainer();
DerivedClass1 derivedClass1 = new DerivedClass1();
derivedClass1.DerivedClass1Prop1 = "DerivedClass1Prop1";
derivedClass1.DerivedClass1Prop2 = "DerivedClass1Prop2";
DerivedClass2 derivedClass2 = new DerivedClass2();
derivedClass2.DerivedClass2Prop1 = "DerivedClass2Prop1";
derivedClass2.DerivedClass2Prop2 = "DerivedClass2Prop2";
container.TestDetails.Add(derivedClass1);
container.TestDetails.Add(derivedClass2);
TestResult testResult = new TestResult();
testResult.TestResultProp1 = "TestResultProp1";
testResult.TestResultProp2 = "TestResultProp2";
container.Data.Add(testResult);
Program p = new Program();
p.MethodIAmWorkingIn(container);
}
private void MethodIAmWorkingIn(TestDetailsAndResultsContainer container)
{
// I need to see if the container variable holds a DerivedClass1 or DerivedClass2 object.
foreach (var result in container.TestDetails)
{
var classINeedToDetermine = container.TestDetails.FirstOrDefault(m => m.TestDetailsProp1 == result.TestDetailsProp1);
if (classINeedToDetermine is DerivedClass1)
{
classINeedToDetermine = result as DerivedClass1;
}
else if (classINeedToDetermine is DerivedClass2)
{
classINeedToDetermine = result as DerivedClass2;
}
// Now I need to use the classINeedToDetermine object and write its specific properties.
// ???????????????????? I am stuck at this point ??????????????????
// I need to write one or the other below. Can this be done?
// If it is DerivedClass1, I need to write out those properties only.
Console.WriteLine(classINeedToDetermine.DerivedClass1Prop1);
Console.WriteLine(classINeedToDetermine.DerivedClass1Prop2);
// OR
// If it is DerivedClass2, I need to write out those properties only.
Console.WriteLine(classINeedToDetermine.DerivedClass2Prop1);
Console.WriteLine(classINeedToDetermine.DerivedClass2Prop2);
}
}
}
public class TestDetailsAndResultsContainer
{
public TestDetailsAndResultsContainer()
{
this.Data = new List<TestResult>();
this.TestDetails = new List<TestDetails>();
}
public List<TestDetails> TestDetails { get; set; }
public List<TestResult> Data { get; set; }
}
public abstract class TestDetails
{
public string TestDetailsProp1 { get; set; }
public string TestDetailsProp2 { get; set; }
}
public class TestResult
{
public string TestResultProp1 { get; set; }
public string TestResultProp2 { get; set; }
}
public class DerivedClass1 : TestDetails
{
public string DerivedClass1Prop1 { get; set; }
public string DerivedClass1Prop2 { get; set; }
}
public class DerivedClass2 : TestDetails
{
public string DerivedClass2Prop1 { get; set; }
public string DerivedClass2Prop2 { get; set; }
}
答案 0 :(得分:2)
我不完全确定这里的对象意味着什么,但它应该是这样的:
foreach(var v in result.GetType().GetProperties())
{
if(v.DeclaringType == result.GetType())
{
Console.WriteLine(v.GetValue(result));
}
}
同样,我对classINeedToDetermine
与result
的使用情况并不完全清楚,但您可以根据自己的需要更改这些内容。我选择了result
作为我的示例,因为代码的那一部分classINeedToDetermine
为空。
顺便说一句,代码的这一部分是多余的。为了清晰和高效,应将其删除。
if (classINeedToDetermine is DerivedClass1)
{
classINeedToDetermine = result as DerivedClass1;
}
else if (classINeedToDetermine is DerivedClass2)
{
classINeedToDetermine = result as DerivedClass2;
}
修改强>
另一方面,如果性能对您来说比可扩展性更重要(读取:如果您知道或控制了您将要处理的所有类),则可以使用此is
要少做一些事情,我讨厌用这个词,&#34;泛型。&#34;
if (classINeedToDetermine is DerivedClass1)
{
var typed = (DerivedClass1)result;
Console.WriteLine(typed.DerivedClass1Prop1);
Console.WriteLine(typed.DerivedClass1Prop2);
}
else if (classINeedToDetermine is DerivedClass2)
{
var typed = (DerivedClass2)result;
Console.WriteLine(typed.DerivedClass2Prop1);
Console.WriteLine(typed.DerivedClass2Prop2);
}
答案 1 :(得分:2)
as
关键字没有按照您的想法执行。
您的classINeedToDetermine
是TestDetails
类型的变量。您可以为其分配子类实例,但仍然无法通过该变量访问该子类特定属性。
你只需要一些范围:
if (classINeedToDetermine is DerivedClass1)
{
var derived1 = (DerivedClass1)result;
Console.WriteLine(derived1.DerivedClass1Prop1);
// etc
}
else if (classINeedToDetermine is DerivedClass2)
{
var derived2 = (DerivedClass2)result;
Console.WriteLine(derived2.DerivedClass2Prop1);
// etc
}
这种事情虽然维持起来并不好玩。
另一种方法是使用多态:您的TestDetails
类可以定义一个名为PrintProperties
的抽象方法,所有子类都可以实现它。
然后你只需要在每个对象上调用PrintProperties
,而不必担心它们属于哪个子类。
答案 2 :(得分:0)
您可以使用Type.GetProperties获取课程的所有属性。然后,您需要通过GetMethod获取Get方法的MethodInfo
。
获得MethodInfo
后,您可以致电Invoke,传递您的派生类。您可以编写然后将结果写入控制台。