Replies: 15 comments
-
The C# compiler currently doesn't know anything about the public static class ExpandoObjectExtensions {
public static void Add(this ExpandoObject target, string key, Object value) {
dynamic d = target;
d[key] = value;
}
} which would enable: var ex = new ExpandoObject() {
{ "a", "foo" },
{ "b", "bar" }
}; Maybe the compiler could consider the target type of the expression as I'd make separate proposals for the other ideas. Check out #1116 which I think is pretty relevant. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
For examole: This is the way Tuples work. ValueTuple Class only have Item1, Item2, Item3... properties. C# converts elemnt names to these properties when it produces IL code. The side effect is that Tuple loose element names through reflection and dynamics. For this I would suggest that C# should produce new fields with the elemnt names inside a new struct inherited from ValueTuple Class. I think this would solve this problem. If this is not a proposal already! |
Beta Was this translation helpful? Give feedback.
-
Changing public class InferedExpandoObject : DynamicObject
{
Dictionary<string, (Type t, object v)> values = new Dictionary<string, (Type, object)>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
bool found = values.TryGetValue(binder.Name, out var tuple);
result = tuple.v;
return found;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
// TODO: handle null value
if (value == null)
throw new NotImplementedException();
Type newType;
if (values.TryGetValue(binder.Name, out var tuple))
{
if (!tuple.t.IsAssignableFrom(value.GetType()))
throw new InvalidOperationException();
newType = tuple.t;
}
else
{
newType = value.GetType();
}
values[binder.Name] = (newType, value);
return true;
}
} If you think such a type would be widely useful, then you should create a proposal about it in the corefx repo, since those types are part of the framework, they're not part of the language. |
Beta Was this translation helpful? Give feedback.
-
@MohammadHamdyGhanem
The problem is that the C# compiler applies the initializers to a temporary variable of the type of the new class, as follows: dynamic d = new ExpandoObject() { a = "a", b = "b" };
// which translates to
ExpandoObject temp = new ExpandoObject();
temp.a = "a";
temp.b = "b";
dynamic d = temp; This is why it doesn't work. Initializers don't consider the type of the target, but I'm suggesting that maybe the compiler could in the case of a
That would be a separate proposal. It would also be impossible because structs cannot be inherited. The approach of creating a new tuple struct was suggested but the team wanted assembly unification and for the names to be ephemeral, so we ended up with the result of that decision. If the goal is to enable object initializer syntax with objects intended to be used with dynamic e = new IndexableExpandoObject() {
["a"] = "foo",
["b"] = "bar"
}; |
Beta Was this translation helpful? Give feedback.
-
@svick |
Beta Was this translation helpful? Give feedback.
-
To throw some random syntax spaghetti against the wall, how about the var d = new dynamic ExpandoObject() {
a = "foo",
["b"] = "bar"
};
// translates to
dynamic d = new ExpandoObject();
d.a = "foo";
d["b"] = "bar"; Although I can't honestly imagine that this comes up often. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
What are you actually trying to achieve? This sounds like you're programming a bunch of goop. |
Beta Was this translation helpful? Give feedback.
-
Yes. I was specifically talking about point 2 of the original proposal, which I think belongs to corefx. Point 1 does belong here. (And point 3 belongs to the roslyn repo, since that's not the language, just IntelliSense.) |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h |
Beta Was this translation helpful? Give feedback.
-
I will go now to complain about invoking ExpandoObject dynamic delegates in VB.NET. It reguires the Invoke method unlike C#!.. This is a rare situation where C# syntax wins! |
Beta Was this translation helpful? Give feedback.
-
By the way. I want to suggest to change the Controller.ViewBag property In ASP.NET. I think in will be easier and more efficient to declare it as a ValueTuple instead of ExpandoObject. The code will be sorter and strong typed and we will have intilisense support in razor. Or Create a new bag property of such kind, not to miss with old projects. |
Beta Was this translation helpful? Give feedback.
-
If you want strong typing, use a strongly typed model with |
Beta Was this translation helpful? Give feedback.
-
May be this proposal give a solution: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If there's still a significant importance of the ExpandoObject after ValueTuple syntax added to C#, I suggest to enhance ExpandoObject like this:
This is much better than:
Beta Was this translation helpful? Give feedback.
All reactions