Click here to Skip to main content
15,888,290 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get a red wiggly line under 'string' in the Import section (marked with a strikethrough in my code below) with the error "Expected class, delegate, enum, interface, or struct"

anyone know where im going wrong



namespace MEFProgram
{
    [Import (typeof(IMessage))]
    public string GetMessage {get;set;}


    class Program
    {
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.Compose();
        }

        void Compose()
        {
            var catalog = new TypeCatalog();
            var container = new CompositionContainer();
            container.ComposeParts(catalog);
            Console.WriteLine(GetMessage);
            Console.ReadLine();
        }
    }
}

class plugIN
{
    [Export(typeof(IMessage))]
    string GiveMessage
    {
        get
        {
            return "Message Inserted by MEF";
        }
    }
}
Posted

C#
public string GetMessage {get;set;}


Is not within a class you can not have a getter ans setter in the namespace.
 
Share this answer
 
Comments
webpeon80 30-Nov-11 15:44pm    
so where would I place that code if I needed it to be an Import using MEF
Should the attribute not go above the class ? Just a guess..

C#
namespace MEFProgram
{
    public string GetMessage {get;set;}
 

    [Import (typeof(IMessage))]
    class Program
    {
 
Share this answer
 
This is often caused by an extra a closing paranthesis.
Refer to the below answer for more information.
Expected class,delegate,enum, interface,or struct[^]
 
Share this answer
 
"Expected class, delegate, enum, interface, or struct"

But you have a simple method!
 
Share this answer
 
sorted... dropped the string line inside the program class and it cleared up, final code incase anyone runs into the same problem

namespace MEFProgram
{
    class Program
    {
        [Import()]
        string GetMessage { get; set; }

        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.Compose();
        }

        void Compose()
        {
            AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            CompositionContainer container = new CompositionContainer(catalog);
            container.ComposeParts(this);
            Console.WriteLine(GetMessage);
            Console.ReadLine();
        }
    }
}
 
Share this answer
 
The property GetMessage is outside the class.At the namespace level there can be class,enum,interface or struct.

Properties should be defined at class level
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900