C# 7.0 Value Tuple compile error? - asp.net

When I am trying to compile the following code:
var post = iPostService.GetAll().Select(x => (x.Title, x.Author));
I get the compiler error: 'An expression tree may not contain a tuple literal.'
So I also tried this:
var post = iPostService.GetAll().
Select(x => new ValueTuple<string, string>(x.Title, x.Author))
The result is runtime error:'Cannot resolve method Void .ctor(System.String, System.String) because the declaring type of the method handle System.ValueTuple`2[T1,T2] is generic. Explicitly provide the declaring type to GetMethodFromHandle.'
I googled to find out the solution to this problem but nothing really helpful.
Any help really appreciated.
Thanks

It works for me, create a tuple first and convert it to a ValueTuple:
var post = iPostService.GetAll().
Select(x => new Tuple<string, string>(x.Title, x.Author).ToValueTuple())

Finally, I found out what wrong with my code:
I am using deferred execution so data won't load from the database when the constructor executed.
The solution is to add conversion operators before creating instance command.
Hope it works with your code.

Related

Object.class.getMethod("equals", Object.class) doesn't work with BeanShell

Object.class.getMethod(methodName, Object.class);
is not getting executed by bsh.Interpreter and throwing exceptions
Typed variable declaration : reflection error: bsh.ReflectError: Method getMethod( java.lang.String, java.lang.Class ) not found in class'java.lang.Class'
Any idea how to fix this ?
Thanks in advance.
Beanshell doesn't support variable parameters, so you need to put it in an array as per older versions of the Java language.
Object.class.getMethod("equals", new Class[] { Object.class } );

TypeError: Error #1034: Type Coercion failed: cannot convert Object#1456c7b9 to mx.messaging.messages.IMessage

Im trying to connect a Flash client to BlazeDS. There has been some success with this from others using the vanilla BlazeDS setup. However I'm using the new Spring BlazeDS Integration from springsource and running aground.
The flash client actually seems to be working in that I can see the correct data in the body of the returned object, but for some reason unknown it fails casting as an IMessage. It fails in PollingChannel.as on this line with the subject line error
var messageList:Array = msg.body as Array;
for each (var message:IMessage in messageList) <--
On application load I register a whole bunch of classes like so
registerClassAlias( "flex.messaging.messages.RemotingMessage", RemotingMessage );
registerClassAlias("mx.messaging.messages.IMessage", IMessage);
etc..
my code is basically
var channelSet:mx.messaging.ChannelSet = new mx.messaging.ChannelSet();
var channel:mx.messaging.channels.AMFChannel = new AMFChannel("my-amf", "http://localhost:8400/SpringA/messagebroker/amf");
channelSet.addChannel(channel);
var consumer:mx.messaging.Consumer = new Consumer();
consumer.channelSet = channelSet;
consumer.destination = "simple-feed";
consumer.subscribe();
consumer.addEventListener(MessageEvent.MESSAGE, test);
private function test(event:IMessage)
{
trace("msg..");
// breakpoint never makes it here
}
I have a flex client which works 100% with same destination/channel.
The error in the title means that you, for some reason, got an object that is not implementing or extending the IMessage interface, therefore the loop can not cast it in this part:
for each (var message:IMessage in messageList){
Either you should somehow make sure that you don't add anything that is not extending or implementing IMessage, or check if the variable IS actually ext./imp. it. Also - if you want to do that, you will have to change the for each like this:
for each (var obj in messageList){
if (obj is IMessage){
var message:IMessage = obj as IMessage;
// DO STUFF HERE
}
}
Add this Object mapping:
registerClassAlias("flex.messaging.io.ObjectProxy", ObjectProxy);
If on your Java VO objects you have overridden the hashcode() method, this situation could happen.
Remove the hashcode() override (if you are able to).
See my blog for the backstory on how I discovered this. http://squaredi.blogspot.com/2013/12/remoting-landmine-without-stack-trace.html
I had the same error when trying to send an actionscript object to the backend. My problem was that my c# equivalent object was missing an public parameterless constructor.

ActionScript 3: Use an argument as a type?

I have a movie clip in my library linked with a class name "MyClass", and I am trying to do something like this in Actionscript 3:
function createbtn(bclass:Class):void{
var addB:bclass = new bclass();
addChild(addB);
}
creatbtn(MyClass);
But, I get this error: "1046: Type was not found or was not a compile-time constant: bclass."
Thank you very much in advanced.
Close, the type of the variable is wrong.
function createbtn(bclass:Class):void{
var addB:* = new bclass();
addChild(addB);
}
creatbtn(MyClass);
Since you don't know the type, just mark it with a * so the compiler knows that it can be any type. You might want to do some type checking though, since you're adding it to the display list. Then you could probably type it as DisplayObject.

MOQ Testing return types

Given the following:
var mockIActionService = new Mock<IActionService>();
var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object);
mockValidAgeRule.Setup(t => t.Execute(app));
Now t.Execute returns a "Rules" object, how can I verify that something has been called on Rules?
I am attempting to call it as such mockValidAgeRule.Verify(x => x.Execute(app).Passed)
I want to test that the object Result returned true given the inputs.
Sorry for all questions just am having a little trouble finding info about MOQ that is up to date and helpful
Well, as stated by Chris, you haven't provided enough detail to get a proper answer. Nonetheless, IMHO it's pretty clear that this test has a code smell. It don't appear to have any concrete implementations. A test completely composed of mock objects is not likely testing anything useful.
Which class represents your SUT? It sounds like it might be your Rules object. If you provide further detail about your object model and expected behaviors, it would be easier to provide a feedback.
I suggest to add RulesMock object something like this:
var rulesMock = new Mock<Rules>();
rulesMock.SetUp(x => x.MethodInRules).Return(some_object);
then add this mock to your code:
var mockIActionService = new Mock<IActionService>();
var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object);
mockValidAgeRule.Setup(t => t.Execute(app)).Returns(rulesMock.Object);
so, if your Rules getter call MethodInRules() you can check this is called by:
rulesMock.Verify(x => x.MethodInRules, Times.Once);
It is an idea, hope this helps someone, good luck!

Answering "Which method called me?" at the run-time in .NET? Or is CallStack data readable by the code?

Presume that there are methodA() , methodB() and methodC().
And methodC() is called at the run-time.
Is is possible to know methodC() is called from what method?
I was thinking if CallStack can be read at the run-time for some checks? If yes, I think it should not be a big deal.
Any ideas?
Thanks!
Use the StackTrace and StackFrame classes. For example:
StackTrace stackTrace = new StackTrace();
StackFrame[] stackFrames = stackTrace.GetFrames();
foreach (StackFrame stackFrame in stackFrames)
{
string method = stackFrame.GetMethod().Name;
// do some stuff with method
}
Yes, the call stack can be read at runtime using StackTrace.Get­Frames.

Resources