Jump to content

LINQ woes...


Recommended Posts

So I've been looking through alot of examples and answers posted here on the forums (by the way, thanks for all the help everyone, whether you know it or not! :) ) and much of the code I've seen has been using LINQ, where you can do stuff like Collection.Where(i => i.equals(something)); or SelectAll(i => {someList.Add(new Object(i));} instead of classic for and while loops.

However, when I attempt to use this style syntax, explosions of Kerbal proportions happen in my code (MissingMethodException, TypeLoadException, etc.), whereas classic foreach loops work like a charm. The latest example of this was me attempting to use Reflection to create some objects.

moduleList = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes().Where(type => type.IsSubclassOf(typeof(SubterraneanBaseModule))))
.Select(type => { return (SubterraneanBaseModule)Activator.CreateInstance(type); }).ToList<SubterraneanBaseModule>();

Blows up horrifically, failing to load the types from my .dll

foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type t in assembly.GetTypes())
{
if (t.IsSubclassOf(typeof(SubterraneanBaseModule)))
{
moduleList.Add(Activator.CreateInstance(t) as SubterraneanBaseModule);
}
}
}

This however, works perfectly.

While I can deal with doing stuff the classic way, it makes me wonder if I've not set up my IDE correctly to build things properly for Unity to handle LINQ properly. What is everyone else doing to set up their environments that I omitted?

Link to comment
Share on other sites

  • 1 month later...
This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...