C# - Help with LINQ
Posted
by cmaduro
on Stack Overflow
See other posts from Stack Overflow
or by cmaduro
Published on 2010-05-21T01:07:20Z
Indexed on
2010/05/21
1:10 UTC
Read the original article
Hit count: 445
I need to check if a certain property exists within a class. Please refer to the LINQ query in question. For the life of me I cannot make the compiler happy.
class Program
{
static void Main(string[] args)
{
ModuleManager m = new ModuleManager();
IModule module = m.FindModuleForView(typeof(HomeView));
Console.WriteLine(module.GetType().ToString());
Console.ReadLine();
}
}
public class ModuleManager
{
[ImportMany]
public IEnumerable<Lazy<IModule>> Modules { get; set; }
[ImportMany]
public IEnumerable<Lazy<View>> Views { get; set; }
public ModuleManager()
{
//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);
//Fill the imports of this object
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
public IModule FindModuleForView(Type view)
{
//THIS IS THE PROBLEM
var module = from m in Modules
where (
from p in m.Value.GetType().GetProperties()
where p.GetType().Equals(view)
select p
)
select m;
}
public CompositionContainer _container { get; set; }
}
public interface IModule
{
}
[Export]
public class HomeModule : IModule
{
public HomeModule()
{
}
[Export]
public HomeView MyHomeView
{
get
{
return new HomeView();
}
set
{
}
}
}
public class HomeView : View
{
}
public class View
{
}
© Stack Overflow or respective owner