CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Posts
    9

    [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?

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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...

  3. #3
    Join Date
    Feb 2010
    Posts
    9

    Re: LINQ, oh the joy.

    Quote Originally Posted by nelo View Post
    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)

  4. #4
    Join Date
    Feb 2010
    Posts
    9

    Re: LINQ, oh the joy.

    Quote Originally Posted by jgshojr View Post
    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.

  5. #5
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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
  •  





Click Here to Expand Forum to Full Width

Featured