I'd like to define a psuedo strongly typed object but still be able to define it inline.
current solution:
callMethod({param1:paramvalue,param2:paramvalue2});
I'd like to use similar inline syntax but the parameter 'object' to only contain a specific list of parameters. IE: only allow param1 & param2 but not param3.
I'd be happy with something like this:
callMethod(mytype(param1:paramvalue,param2:paramvalue2);
This is what you want:
callMethod( new MyType(paramvalue, paramvalue2) );
Related
I have a naive question about using multiple attributes with GNU style.
The specification says:
An attribute specifier is of the form attribute ((attribute-list)). An attribute list is a possibly empty comma-separated sequence of attributes.
Now, suppose I want to annotate a function using two "attribute((annotate("xxx")))"
I can do it like that:
void __attribute__((annotate("a"))) __attribute__((annotate("b"))) fff(){}
But, it seems that I cannot do it like that:
void __attribute__(annotate("a"), annotate("b")) fff(){}
I was wondering how can I do it like the second form since it is more concise.
Thanks in advance!
You're almost there - just add another pair of brackets:
void __attribute__((annotate("a"), annotate("b"))) fff(){}
// ^ ^
I've found properties corresponding to each action named like this: MVC.<Controller>.<Action>Params, they contain parameter names for each action. What are they for and how they can be used?
There were some edge scenarios where it was interesting to pass the parameter name as a constant. I can't instantly recall what that person was doing, but I could see this being useful is calls to AddRouteValue. In the end, it's all about never to have to use a literal string that refers to a C# object, whether it's a class, method, or param.
I see that mvc is finding the names of the variables passed to it from the lambda function in an Html.DisplayFor method:
#Html.HiddenFor(model => myModel.propA.propB)
could generate HTML something like:
<input id="myModel_propA_propB" type="hidden" value="" >
it is obviously using reflection, but it is beyond me. could someone fill me in?
ALSO, is it possible to create an HTML helper function that takes a fully property reference instead of a lambda function to accomplish something similar? ie.
#Html.HiddenFor(myModel.propA.propB)
...and the helper could be passed the full "myModel.propA.propB" reference and not just the value of propB? is a lambda function an odd .net workaround to accomplish this sort of task or is it actually the preferred approach across all programming disciplines.
Actually, it is traversing the Expression tree you pass into helper method in order to get the property names. In practice, it would look something like:
MemberExpression memberExpression = (MemberExpression) expression.Body;
propertyName = memberExpression.Member.Name;
Of course that is not complete - for instance, you would have to walk up the chain of Expressions when there are multiple property invocations in the expression passed in, you would have to account for other expression types being passed in than MemberExpression, etc., etc. - but you get the idea. Remember that an Expression is a code expression represented as data. Also, since MVC is open source, you could look up the exact code they use to arrive at the html name in the sources, if you want.
To address your second question, the answer is no. Passing "just the property" without the lambda (which will be an Expression<Func<T,object>>), will not work, because then the function can only see the value passed in - and nothing about how the calling code arrived at that value.
I like to use the "this" keyword when referencing auto-implemented properties and when calling methods inside my class. How do I change the CodeRush templates for "Convert to auto-implemented property" and "Extract Method" to append the "this" keyword to the refactored references?
This is not possible at the moment. But, most likely, this ability will appear in the near future.
Given a spark view named SomeContainer.spark that uses a partial view this way:
<SomeContent param1 = "Model.SomeValue"/>
and given a partial view named SomeContent.spark that uses the parameter this way:
<div>${param1}</div>
How can I modify SomeContent.spark to declare param1 upfront. I want to do that for two reasons:
Readability: readers will know what the partial view depends on
To get intellisence for param1 in Visual Studio
I tried to simply declare the same <var> in SomeContent.spark but it fails at runtime indicating that that variable already exists.
I got the answer from the Spark group. In the partial you can declare a variable using the <default/> element:
<default param1="new List<string>()" type="List[[string]]"/>
Not only does it declare the parameter (with the advantages mentioned in my question) but it also gives it a default value which can be used to prevent the partial form getting a NullReferenceException...