Console.WriteLine is not working after Resolve()
Asked Answered
C

1

0

This code gives no error or warning during execution. but it ignores the console.read() function, i am newer with windsor. is it really a bug or the simple behavior of windsor ?

using System;
using Castle.Windsor;
using Castle.MicroKernel.Registration;

namespace CastleProject
{
class Program
{
    internal interface ILogger
    {
        void log(string message);
        void showMethod();
    }
    internal interface Ishowing
    {
        void checkInterface();
    }

    class Logger : ILogger
    {
        public void log(string message)
        {
            Console.WriteLine(message);
            Console.Read();
        }
        public void showMethod()
        {
            Console.WriteLine("This is again showing just function i existing");
        }

    }
    class Showing : Ishowing
    {
        public void checkInterface()
        {
            Console.WriteLine("this is line from checkInterface()");
            var a = Console.ReadLine();
            Console.WriteLine(a);
        }
    }

    static void Main(string[] args)
    {
        var container = new WindsorContainer();
        container.Register(Component.For<ILogger>().ImplementedBy<Logger>(),Component.For<Ishowing>().ImplementedBy<Showing>());
        var logger = container.Resolve<ILogger>();
        var logger2 = container.Resolve<Ishowing>();
        logger.log("hello message");
        logger.showMethod();
        logger2.checkInterface();          
    }
}

}

Crack answered 3/2, 2013 at 5:25 Comment(0)
R
1

I think you probably want to use Console.ReadLine rather than Console.Read.

Try this snippet of code to understand Read/ReadLine behavior:

var a = Console.Read();
Console.WriteLine(a);
var b = Console.ReadLine();            
Console.WriteLine(b);
Rigi answered 3/2, 2013 at 5:34 Comment(5)
yeah it worked, but its a bit odd. Console.Read() suppose to read a char where as ReadLine suppose to read a line, is there any garbage value which effects the process ?Crack
This is just how it works. Console.Read() will wait for enter, but it will read only one character from the input stream. Therefore rest of text and 'enter' will stay in input stream. Then if you have Console.ReadLine() after, it will just read the rest of the stream and will not wait for 'enter' again. Obviously that has nothing to do with Castle Windsor.Rigi
one thing more .. in above code can we use same variable logger to resolve both ILogger and Ishowing? I tried but not able to do that.Crack
You could do that if you created new interface, like IShowingLogger inheriting from both IShowing and ILogger, then you can make your logger class implement that interface, and then you can resolve for it. Resolve method returns single interface, so I think that would be the only way to do it.Rigi
i have one question more can we register one interface with two classes as the concept of polymerphism, as we used using virtual function for polymerphism.Crack

© 2022 - 2024 — McMap. All rights reserved.