Eder
Eder

Reputation: 1884

Creating anonymous types!

I know that I can do something like this:

var test = new { FirstName = string.Empty, LastName = string.empty };

But I don't if there's is a way to do it dynamically, let's say:

var test = new {};
if (condition) {
    test.Property = string.Empty;
}

Let's say I Have this:

string[] names = 
{
    "eder",
    "quiñones",
    "quoe840629",
    "3301"
};

var anonymous = new {};
foreach (string name in names) {
    // Create anonymous types...
}

Any suggestions?

~ Eder Quiñones

Upvotes: 2

Views: 813

Answers (1)

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 121057

You can do:

var test = new { FirstName = (condition ? string.Emtpy : "other") }; 

If you were using c# 4 you could use a dynamic type (ExpandoObject).

Upvotes: 7

Related Questions