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

百般思念 提交于 2021-02-04 21:10:37

问题


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". So, displayProperty3 would have a value of null. But I want to eliminate displayProperty3 so that if selected is "A", then there should be only 2 properties in the object.

If there is any proper and efficient way to do this, I would be grateful for it.


回答1:


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 (e.g. 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.

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
}



回答2:


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.




回答3:


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.




回答4:


Just because anonymous objects (and var) are not explicitly typed does not mean they don't have types. 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.




回答5:


You should take a look at dynamic instead of var:

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

Var is a way to create a new strongly typed entity at compile time. Dynamic is not strongly typed and properties can be added/removed during code execution.



来源:https://stackoverflow.com/questions/14365858/is-it-possible-to-modify-or-remove-an-anonymous-type-from-an-object-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!