Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

LINQ and Reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
wildThought - 25 Sep 2007 01:50 GMT
If I get an object generated by SQL Metal I can do a call like:

db.Persons.Add(new Person(){name = "ED"};

So far, so good.  However, we are loading a DB from MetaData, I need
to do this via reflection.

If I do db.GetType().GetProperty("Persons")

No Problem. I get the right object.

Now, when I do
db.GetType().GetProperty("Persons").GetType().GetMethods()

The Add method is not reflected, I wanted to be able to Dynamically
Invoke this object, but it does not show up as avalid Method.

Any Thoughts?
Jon Skeet [C# MVP] - 25 Sep 2007 08:25 GMT
> If I get an object generated by SQL Metal I can do a call like:
>
[quoted text clipped - 14 lines]
>
> Any Thoughts?

Yes - GetProperty returns a PropertyInfo, and you're calling GetType()
on that. You should be using

db.GetType().GetProperty("Persons").PropertyType.GetMethods()

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Ben Voigt [C++ MVP] - 26 Sep 2007 23:39 GMT
>> If I get an object generated by SQL Metal I can do a call like:
>>
[quoted text clipped - 19 lines]
>
> db.GetType().GetProperty("Persons").PropertyType.GetMethods()

Moreover, you likely want to use the override of GetMethods that accepts an
argument.  From its doc page:

The following BindingFlags filter flags can be used to define which methods
to include in the search:

 a.. You must specify either BindingFlags.Instance or BindingFlags.Static
in order to get a return.

 b.. Specify BindingFlags.Public to include public methods in the search.

 c.. Specify BindingFlags.NonPublic to include non-public methods (that is,
private and protected members) in the search.

 d.. Specify BindingFlags.FlattenHierarchy to include public and protected
static members up the hierarchy; private static members in inherited classes
are not included.

The following BindingFlags modifier flags can be used to change how the
search works:

 a.. BindingFlags.DeclaredOnly to search only the methods declared on the
Type, not methods that were simply inherited.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.