MEF Proramming in Visual C# 2010 Express: Doing "LINQing to MEF Imports" - One Error: no extension method "Name" accepting a 1st argument of type 'scMEF_Example3.IMessage?
-
18 Juni 2012 18:01
Hi all,
I followed the James Eggers’ instructions “LINQing to MEF Imports” (from http://randomactsofcoding.blogspot.com/2009/11linqing-to-mef-imports.html) to launch scMEF-Example3 in my Visual C# 2010 Express - see the following code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection; using System.Runtime.Remoting; using System.Runtime.Remoting.Messaging; namespace scMEF_Example3 { public interface IMessage { string MyMessage { get; } } public class Program { [ImportMany(typeof(IMessage))] IEnumerable<IMessage> Messages { get; set; } static void Main(string[] args) { Program p = new Program(); p.Run(); } void Run() { Compose(); IMessage message = (Messages.Where(m => m.Name == "Simple")).FirstOrDefault; Console.WriteLine(message.MyMessage); Console.ReadKey(); } private void Compose() { var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); } } // class SimpleMessage and class SecondMessage [Export(typeof(IMessage))] public class SimpleMessage : IMessage { public string MyMessage { get { return "Eggers: LINQ to ImportMany - Hello World of SimpleMessage Class!"; } } } [Export(typeof(IMessage))] public class SecondMessage : IMessage { public string MyMessage { get { return "Eggers: LINQ to ImportMany - Hello LINQ derived ImportMany, From a Second Class"; } } public string Name { get { return "Second"; } } } }
I got an error in this console project :1 Error 'scMEF_Example3.IMessage' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'scMEF_Example3.IMessage' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scMEF-Example3\Program.cs 32 55 scMEF-Example3.
Please help and tell me what causes this error and how to reslve it.
Thanks,
Scott Chang
Semua Balasan
-
18 Juni 2012 18:09
You need to add the "Name" property to IMessage if you want to be able to use it in your LINQ query:
public interface IMessage { // Add this here... string Name { get; } string MyMessage { get; } }
Note that, once you do that, you'll also have to make SimpleMessage implement it (and return "Simple" for the query to work).
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Disarankan sebagai Jawaban oleh Reed Copsey, JrMVP 18 Juni 2012 20:28
-
18 Juni 2012 18:23
Hi Reed, Thanks for your response.
I added what you instructed. The new code is listed below:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection; using System.Runtime.Remoting; using System.Runtime.Remoting.Messaging; namespace scMEF_Example3 { public interface IMessage { string MyMessage { get; } string Name { get; } //newly added per Reed C. } public class Program { [ImportMany(typeof(IMessage))] IEnumerable<IMessage> Messages { get; set; } static void Main(string[] args) { Program p = new Program(); p.Run(); } void Run() { Compose(); IMessage message = (Messages.Where(m => m.Name == "Simple")).FirstOrDefault; Console.WriteLine(message.MyMessage); Console.ReadKey(); } private void Compose() { var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); } } // class SimpleMessage and class SecondMessage [Export(typeof(IMessage))] public class SimpleMessage : IMessage { public string MyMessage { get { return "Eggers: LINQ to ImportMany - Hello World of SimpleMessage Class!"; } } } [Export(typeof(IMessage))] public class SecondMessage : IMessage { public string MyMessage { get { return "Eggers: LINQ to ImportMany - Hello LINQ derived ImportMany, From a Second Class"; } } public string Name { get { return "Second"; } } } }I got a new error: 1 Error Cannot convert method group 'FirstOrDefault' to non-delegate type 'scMEF_Example3.IMessage'. Did you intend to invoke the method? C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scMEF-Example3\Program.cs 33 32 scMEF-Example3.
I don't know how to reslve this new error. Please help me and tell me how to correct this new error.
Thanks again,
Scott Chang
-
18 Juni 2012 18:43
You need to invoke the method - this line:
IMessage message = (Messages.Where(m => m.Name == "Simple")).FirstOrDefault;
Should be:
IMessage message = (Messages.Where(m => m.Name == "Simple")).FirstOrDefault(); // Note the () at the end
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Disarankan sebagai Jawaban oleh Reed Copsey, JrMVP 18 Juni 2012 20:28
-
18 Juni 2012 19:32
Hi Reed,
I don't know what you presented in your last response!!! What do you have in the 2 lines? I can't figure out what they should be. Please clarify them and respond again.
Thanks,
Scott Chang
-
18 Juni 2012 19:40
Hi Reed,
I don't know what you presented in your last response!!! What do you have in the 2 lines? I can't figure out what they should be. Please clarify them and respond again.
Thanks,
Scott Chang
Open the error window, and compile your program. Double click on the error, and Visual Studio will take you to the exact line causing the problem.
If you do that, you'll see where to change where I posted.
That being said, it's here:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection; using System.Runtime.Remoting; using System.Runtime.Remoting.Messaging; namespace scMEF_Example3 { public interface IMessage { string MyMessage { get; } string Name { get; } //newly added per Reed C. } public class Program { [ImportMany(typeof(IMessage))] IEnumerable<IMessage> Messages { get; set; } static void Main(string[] args) { Program p = new Program(); p.Run(); } void Run() { Compose(); IMessage message = (Messages.Where(m => m.Name == "Simple")).FirstOrDefault(); // You need to add () at the end of this line Console.WriteLine(message.MyMessage);
// Leave the rest of your code alone....
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Disarankan sebagai Jawaban oleh Reed Copsey, JrMVP 18 Juni 2012 20:28
-
18 Juni 2012 20:23
Hi Reed,
I added () to the place you said. I got a new, different error: 1 Error 'scMEF_Example3.SimpleMessage' does not implement interface member 'scMEF_Example3.IMessage.Name' C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scMEF-Example3\Program.cs 46 18 scMEF-Example3
I need your help again - please tell me what I should to to reslve this new error.
Many Thanks,
Scott Chang
-
18 Juni 2012 20:28
Hi Reed,
I added () to the place you said. I got a new, different error: 1 Error 'scMEF_Example3.SimpleMessage' does not implement interface member 'scMEF_Example3.IMessage.Name' C:\Documents and Settings\e1enxshc\Local Settings\Application Data\Temporary Projects\scMEF-Example3\Program.cs 46 18 scMEF-Example3
I need your help again - please tell me what I should to to reslve this new error.
Many Thanks,
Scott Chang
Scott,
If you want to learn how to use this - at some point, you'll need to actually look at the errors, and figure out how to solve them. In this case, the problem is that you didn't do what I mentioned in my first response to you:
"you'll also have to make SimpleMessage implement it (and return "Simple" for the query to work)."
Now that you've changed the IMessage interface, SimpleMessage doesn't implement it. You need to add the code for it to do so, like this:// class SimpleMessage and class SecondMessage [Export(typeof(IMessage))] public class SimpleMessage : IMessage { public string MyMessage { get { return "Eggers: LINQ to ImportMany - Hello World of SimpleMessage Class!"; } } public string Name { get { return "Simple"; } } }
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Disarankan sebagai Jawaban oleh Reed Copsey, JrMVP 18 Juni 2012 20:28
- Ditandai sebagai Jawaban oleh Scott_Chang 19 Juni 2012 12:00
-
19 Juni 2012 12:10
Hi Reed, Thanks for your nice response.
I added the code statements you instructed and I executed the newly revised code. It worked nicely. I also tested the following:
IMessage
message = (Messages.Where(m => m.Name == "Second")).FirstOrDefault();
Console.WriteLine(message.MyMessage);
This worked too!!!! Thanks again, Scott Chang