简体   繁体   English

是否可以从c#中的对象修改或删除匿名类型?

[英]Is it possible to modify or remove an anonymous type from an object in c#?

I have a piece of code like below: 我有一段类似下面的代码:

var selected = “A”;
bool isSelected = selected == "A" || selected == "C";
var codeLists = new
{
    displayProperty1 = isSelected ? "property1" : null,
    displayProperty2 = isSelected ? "property2" : null,
    displayProperty3 = selected == "C" ? "property3" : null
};

So, my goal is to eliminate a property if it does not satisfy a condition. 因此,我的目标是消除不满足条件的财产。 In the above code, selected is "A" . 在上面的代码中, selected"A" So, displayProperty3 would have a value of null . 因此, displayProperty3的值为null But I want to eliminate displayProperty3 so that if selected is "A", then there should be only 2 properties in the object. 但是我想消除displayProperty3以便如果选择的是“ A”,则对象中应该只有2 properties

If there is any proper and efficient way to do this, I would be grateful for it. 如果有任何适当有效的方法可以做到这一点,我将不胜感激。

I don't think you want to remove the property. 我认为您不想删除该属性。 What you really want to do is have a test on the UI that doesn't display something if its null. 您真正想要做的是在UI上进行测试,如果该测试为null,则该测试不会显示任何内容。

No, an anonymous type still follows the rules of other types, they're just not explicitly defined at compile-time. 不,匿名类型仍然遵循其他类型的规则,只是在编译时未明确定义它们。 To do what you want you'd have to define two different types. 要执行您想要的操作,您必须定义两种不同的类型。

If you don't want to show that property in your UI (eg if you ware binding to a grid that's auto-generated and you don't want that to be a column) then deal with that in your UI. 如果您不想在UI中显示该属性(例如,如果您绑定到自动生成的网格并且您不希望将其作为列),则可以在UI中处理该属性。

However, if you HAVE to do this, you'd have to create two different types (either anonymous or explicit): 但是,如果必须执行此操作,则必须创建两种不同的类型(匿名或显式):

var selected = "A";
bool isSelected = selected == "A" || selected == "C";
dynamic codeLists;
if(selected == "C")
{
    codeLists = new
    {
        displayProperty1 = isSelected ? "property1" : null,
        displayProperty2 = isSelected ? "property2" : null
    }; 
}
else
{
    codeLists = new
    {
        displayProperty1 = isSelected ? "property1" : null,
        displayProperty2 = isSelected ? "property2" : null,
        displayProperty3 = "property3" 
    }; 
}

It would be better if you created a base type with the common properties, but either way they are going to be two different types: 如果创建具有公共属性的基本类型会更好 ,但是无论哪种方式,它们都将是两种不同的类型:

public class CodeList
{
    public string displayProperty1 {get; set;}
    public string displayProperty2 {get; set;}
}

public class CodeListC : CodeList
{
    public string displayProperty3 {get; set;}
    // Other two properties will be inherited
}

If I understood correctly, the object you are building should represent somehow an interface right? 如果我正确理解,您正在构建的对象应该以某种方式表示接口,对吗? Now, building an interface based on the presence/absence of properties doesn't sound very appealing to me, I think it's a terrible idea actually as your code would become a nightmare. 现在,基于属性的存在/不存在来构建接口对我来说并不是很吸引人,我认为这实际上是一个糟糕的主意,因为您的代码将成为一场噩梦。
I think you should revise your approach. 我认为您应该修改方法。 Have you considered using for example a dictionary ? 您是否考虑过使用字典 You can still check if something is there or not plus the code needed to handle that will be much simpler (first of all, no reflection...). 您仍然可以检查是否存在某些东西,再加上处理所需的代码,这将变得更加简单(首先,没有任何反射...)。
Here's the equivalent piece of code of what you posted in your question: 这是您在问题中发布的内容的等效代码:

var selected = “A”;
bool isSelected = selected == "A" || selected == "C";

var codeList = new Dictionary<string, string>();
if(isSelected) {
    codeList["displayProperty1"] = "property1";
    codeList["displayProperty2"] = "property2";
}

if(selected == "C")
    codeList["displayProperty3"] = "property3";

If !isSelected the dictionary won't contain the keys called displayProperty1 and displayProperty2. 如果!isSelected,则词典将不包含名为displayProperty1和displayProperty2的键。

Just because anonymous objects (and var ) are not explicitly typed does not mean they don't have types. 仅仅因为未显式键入匿名对象(和var )并不意味着它们没有类型。 The type needs to be able to be completely defined at compile time, or your code won't build. 该类型需要能够在编译时完全定义,否则您的代码将无法生成。

Since an object of a type with properties displayProperty1 and displayProperty2 is different from one of a type with those two plus displayProperty3 , then you can't try to stick them both into the same variable, any more than you can put a string and an int into the same one. 由于与性能的类型的对象displayProperty1displayProperty2是不同的类型,一个与这两个加displayProperty3 ,那么你就不能试着他们俩坚持到相同的变量,任何比你更可以把一个stringint变成同一个

You should take a look at dynamic instead of var: 您应该看一下动态而不是var:

http://msdn.microsoft.com/en-us/library/dd264736.aspx http://msdn.microsoft.com/en-us/library/dd264736.aspx

Var is a way to create a new strongly typed entity at compile time. Var是在编译时创建新的强类型实体的方法。 Dynamic is not strongly typed and properties can be added/removed during code execution. 动态类型不是强类型,可以在代码执行期间添加/删除属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM