|
-
February 9th, 2010, 09:00 AM
#1
[RESOLVED] LINQ, oh the joy.
Code:
private IEnumerable<string> GetAllInterfaces(Type[] interfaces)
{
// Todo:
// The GetAllInterfaces method of the TypeWriter class returns a strongly
// typed IEnumerable of strings containing the Name property of each of the
// interfaces types contained in the interfaces parameter.
return from interfaces in typeof(string).GetProperties()
where interfaces.Name == typeof(string)
select interfaces;
}
VS2008, 3.5.
I took a crack at the Todo and am completely off. Guidance please?
-
February 9th, 2010, 09:57 AM
#2
Re: LINQ, oh the joy.
I would do it this way...
Code:
private IEnumerable<string> GetAllInterfaces(Type[] interfaces)
{
return from i in interfaces select i.Name;
}
I'm also new to LINQ so forgive me if it is wrong...
Last edited by nelo; February 9th, 2010 at 09:58 AM.
Reason: replaced 'interface' with 'i' to prevent likely compilation errors...
-
February 9th, 2010, 10:10 AM
#3
Re: LINQ, oh the joy.
 Originally Posted by nelo
I would do it this way...
Code:
private IEnumerable<string> GetAllInterfaces(Type[] interfaces)
{
return from i in interfaces select i.Name;
}
I'm also new to LINQ so forgive me if it is wrong... 
Aha! I came up with something similar, but now I am trying to do a little more complex LINQ statement and now am REALLY stuck.
Code:
private IEnumerable<string> GetAllMembers(Type type)
{
// Todo:
// The GetAllMembers method of the TypeWriter class returns a strongly typed
// IEnumerable of concatenated strings, one for each member of the type
// parameter. Each string contains the member type and signature of the
// related member, separated by a single space. The list is sorted first by
// the member type, then by the name.
return from MemberInfo items in type.GetMembers()
orderby items.GetType
select items.GetType + " ";
// Hint:
// You can get all the members of any type by calling the GetMembers
// method of the Type class.
// Hint:
// You can obtain a member's signature by calling the ToString method
// of the MemberInfo class.
}
That's what I have so far. What am I supposed to do about the memberinfo? Typecast? (confused)
-
February 9th, 2010, 10:17 AM
#4
Re: LINQ, oh the joy.
 Originally Posted by jgshojr
Aha! I came up with something similar, but now I am trying to do a little more complex LINQ statement and now am REALLY stuck.
Code:
private IEnumerable<string> GetAllMembers(Type type)
{
// Todo:
// The GetAllMembers method of the TypeWriter class returns a strongly typed
// IEnumerable of concatenated strings, one for each member of the type
// parameter. Each string contains the member type and signature of the
// related member, separated by a single space. The list is sorted first by
// the member type, then by the name.
return from MemberInfo items in type.GetMembers()
orderby items.GetType
select items.GetType + " ";
// Hint:
// You can get all the members of any type by calling the GetMembers
// method of the Type class.
// Hint:
// You can obtain a member's signature by calling the ToString method
// of the MemberInfo class.
}
That's what I have so far. What am I supposed to do about the memberinfo? Typecast? (confused)
Nevermind, items IS MemberInfo, thus items.ToString() works. DUH.
-
February 9th, 2010, 10:23 AM
#5
Re: [RESOLVED] LINQ, oh the joy.
I would do it like this..
Code:
private IEnumerable<string> GetAllMembers(Type type)
{
from memberInfo in type.GetMembers()
order by memberInfo.GetType().ToString()
select String.Format("{0} {1}", memberInfo.GetType().ToString(), memberInfo.ToString())
}
The description could have been clearer. I'm not sure if they member signature and member name mean the same thing...
I saw your post after I'd put mine. I don't need the GetType().ToString() after all...I'm glad you figured it out.
Last edited by nelo; February 9th, 2010 at 10:24 AM.
Reason: clarification
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|