NET-MEF Programming in Visual C# 2010 Express:Hosting MEF in an Application - 2 errors

Terkunci NET-MEF Programming in Visual C# 2010 Express:Hosting MEF in an Application - 2 errors

  • 25 Juni 2012 17:07
     
      Memiliki Kode

    Hi all,

    I copied the following code from the Microsoft .NET MEF website:

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection; namespace scNET_MEF_1 { [Import] public IMessageSender MessageSender { get; set; } class Program { static void Main(string[] args) { Program p = new Program(); p.Run(); } public void Run() { Compose(); MessageSender.Send("Message Sent"); } private void Compose() { AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); } } public interface IMessageSender { void Send(string message); } [Export(typeof(IMessageSender))] public class EmailSender : IMessageSender { public void Send(string message) { Console.WriteLine(message); } } }

    I got the following 2 errors: 1 Error Expected class, delegate, enum, interface, or struct C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scNET_MEF_1\Program.cs 12 10 scNET_MEF_1
    2 Error The name 'MessageSender' does not exist in the current context C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scNET_MEF_1\Program.cs 23 13 scNET_MEF_1

    Please kindly help and advise me what causes the errors and how to correct the errors.

    Thanks in advance,

    Scott Chang

Semua Balasan

  • 25 Juni 2012 17:57
     
     Jawab Memiliki Kode

    Your property needs to be inside the class:

    namespace scNET_MEF_1
    {
     // This can't be outside of the class - move it
     // [Import]
     //  public IMessageSender MessageSender { get; set; }
    
        class Program
        {
            // Move this inside of the class!
            [Import]
            public IMessageSender MessageSender { get; set; }
    
            static void Main(string[] args)
            {
                Program p = new Program();
                p.Run();
            }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Ditandai sebagai Jawaban oleh Scott_Chang 25 Juni 2012 19:31
    •  
  • 25 Juni 2012 19:49
     
      Memiliki Kode

    Hi Reed, Thanks for your valuable response.

    After I these 2 code statments to the right place, it worked nicely. But the console screen flashed quickly and I could see the console screen.  How can I make the console screen stay on for a little longer?  I added

     public void Run()
            {
                Compose();
                MessageSender.Send("Message Sent");
                Console.WriteLine(MessageSender);  //I added in order to make the console screen stay on longer!!!
                Console, ReadKey();           //I added in order to make the console screen stay on longer!!!
            }

    I got the following errors: 1 Error Only assignment, call, increment, decrement, and new object expressions can be used as a statement C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scNET_MEF_1\Program.cs 32 13 scNET_MEF_1
    2 Error 'System.Console' is a 'type' but is used like a 'variable' C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scNET_MEF_1\Program.cs 32 13 scNET_MEF_1
    3 Error The name 'ReadKey' does not exist in the current context C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scNET_MEF_1\Program.cs 32 22 scNET_MEF_1
    4 Error Invalid expression term ',' C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scNET_MEF_1\Program.cs 32 20 scNET_MEF_1
    5 Error ; expected C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scNET_MEF_1\Program.cs 32 20 scNET_MEF_1
    6 Error ; expected C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scNET_MEF_1\Program.cs 32 22 scNET_MEF_1

    Please help and advise me how to make the console screen stay on a little longer.

    Thannks again,

    Scott Chang

  • 25 Juni 2012 19:57
     
      Memiliki Kode

    You need to use

    Console.ReadKey();

    Not:

    Console, ReadKey(); 

    (Note that yours has a comma, not a period)


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".