向现有对象添加新属性 – c#

huangapple go评论127阅读模式
英文:

Add new property to existing object - c#

问题

当两个 if 条件都满足时,我想将 Start 与值和时间一起添加到相同的 DataList 中。

public List<object> DataList = new List<object>();

if(某些条件)
{     
    DataList.Add(new { Value = Value, Time = time}); // 假设这是 DataList[0]
    //var start = null;
    //DataList.Add(new { Value = Value, Time = time, Start = start}); 
}

if(某些条件)  
{
    -&gt; 我应该如何在这里将 `start` 添加到 DataList[0] 中。
    //DataList.Add(new { Start = start });
    //DataList[0]?.Start  = start;   
}

稍后,当我使用 DataList 时,它应该包含来自两个 if 条件的值。谢谢。

英文:

When both 'if' condition is satisfied, I want Start to be added to the same DataList along with value and time.

public List&lt;object&gt; DataList = new List&lt;object&gt;();

if(somecondition)
{     
    DataList.Add(new { Value = Value, Time = time}); // assume this is DataList[0]
    //var start = null;
    //DataList.Add(new { Value = Value, Time = time, Start = start}); 
}

if(somecondition)  
{
    -&gt; how do I add `start` to DataList[0] here.
    //DataList.Add(new { Start = start });
    //DataList[0]?.Start  = start;   
}

Later, when I use DataList, it should contain values from both if condition. Thank you.

答案1

得分: 1

这里有两个选择。

使用继承:

void 方法()
{
    /* 初始化变量 */

    if (某条件)
    {
        数据列表.Add(new 数据() {  = , 时间 = 时间 });
    }
    else if (另一条件)
    {
        数据列表.Add(new 带起始的数据() {  = , 时间 = 时间, 起始 = 起始 });
    }
}

public List<数据> 数据列表 = new List<数据>();

public class 数据
{
    public string ;
    public string 时间;
}

public class 带起始的数据 : 数据
{
    public string 起始;
}

或者,使用 ExpandoObject

void 方法()
{
    /* 初始化变量 */

    dynamic 数据 = new System.Dynamic.ExpandoObject();

    if (某条件)
    {
        数据. = ;
        数据.时间 = 时间;
    }

    if (另一条件)
    {
        数据.起始 = 起始;
    }

    数据列表.Add(数据);
}

public List<System.Dynamic.ExpandoObject> 数据列表 = new List<System.Dynamic.ExpandoObject>();
英文:

Here are two options.

Use inheritance:

void Method()
{
	/* init variables */

	if (someCondition)
	{
		DataList.Add(new Data() { Value = Value, Time = time });
	}
	else if (someOtherCondition)
	{
	    DataList.Add(new DataWithStart() { Value = Value, Time = time, Start = start });
	}
}

public List&lt;Data&gt; DataList = new List&lt;Data&gt;();

public class Data
{
	public string Value;
	public string Time;
}

public class DataWithStart : Data
{
	public string Start;
}

Or, use ExpandoObject:

void Method()
{
	/* init variables */

	dynamic data = new System.Dynamic.ExpandoObject();

	if (someCondition)
	{
		data.Value = Value;
		data.Time = time;
	}
	
	if (someOtherCondition)
	{
	    data.Start = start;
	}
	
	DataList.Add(data);
}

public List&lt;System.Dynamic.ExpandoObject&gt; DataList = new List&lt;System.Dynamic.ExpandoObject&gt;();

答案2

得分: 0

使用System.Collections.Generic和System.Dynamic;

List DataList = new List();

dynamic data1 = new ExpandoObject();

如果 (某些条件)
{
data1.Value = Value;
data1.Time = time;
}

如果 (某些条件)
{
data1.Start = start;
}

DataList.Add(data1);

你可以用ExpandoObject以最简单的方式解决。

英文:
using System.Collections.Generic;
using System.Dynamic;

List&lt;dynamic&gt; DataList = new List&lt;dynamic&gt;();

dynamic data1 = new ExpandoObject();

if (someCondition)
{
    data1.Value = Value;
    data1.Time = time;
}

if (someCondition)
{
    data1.Start = start;
}

DataList.Add(data1);

you can use ExpandoObject to solve in simplest way.

huangapple
  • 本文由 发表于 2023年4月11日 16:04:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定