Call generic parameter methods through reflection

Source: Internet
Author: User

I recently learned about EDM and found that LinqPad is a good thing. It can run the Linq To SQL and Entity SQL Language scripts, but does not use the ESql query function. It always reports an error and cannot find the portal for half a day, in anger, I decided to write a small ESql queryer myself.

 

Summarize important knowledge points and paste some important code to avoid forgetting them next time.

 

Private void btnRunQuery_Click (object sender, EventArgs e)
{
If (cmbEntitySet. SelectedItem = null)
Return;
Type typeObjectQuery = (cmbEntitySet. SelectedItem as ImageComboBoxItem). Value as Type;
Type typeEntity = typeObjectQuery. GetGenericArguments () [0];

Using (cdms3Entities ctx = new cdms3Entities ())
{
// Call ObjectQuery through reflection <T> CreateQuery <T> (string queryString, params ObjectParameter [] parameters );
MethodInfo miCreateQuery = ctx. GetType (). GetMethod ("CreateQuery", BindingFlags. Instance | BindingFlags. Public );
// Set the T generic parameter in CreateQuery <T> to the object type specified by typeEntity.
MiCreateQuery = miCreateQuery. MakeGenericMethod (typeEntity );
// Create a call parameter and input the Entity SQL Language statement
Object [] args = new object [] {mmeESql. Text, new ObjectParameter [] {}};
// Call the CreateQuery function to return an object of the ObjectQuery <T> type. The ObjectQuery <T> generic class is derived from the ObjectQuery class, so it can be converted to the ObjectQuery type.
ObjectQuery oq = miCreateQuery. Invoke (ctx, args) as ObjectQuery;

// If any of the following check boxes needs to Include objects in the query
If (cmbIncludes. Properties. GetCheckedItems ()! = "")
{
// Obtain the ObjectQuery <T> Include (string path); function. Note that the return value type of the function is generic, but the function is not generic, therefore, you do not need to call the MakeGenericMethod method.
MethodInfo miInclude = oq. GetType (). GetMethod ("Include", BindingFlags. Instance | BindingFlags. Public );
Foreach (CheckedListBoxItem item in cmbIncludes. Properties. Items)
{
If (item. CheckState = CheckState. Checked)
{
EntityObjectInfo eo = item. Value as EntityObjectInfo;
Args = new object [] {eo. Path };
// Call ObjectQuery <T>. Include Method
Oq = miInclude. Invoke (oq, args) as ObjectQuery;
}
}
}

// Call ObjectQuery <T>. ToList () to obtain the query result.
// Note that the ToList () method is an extension method. Its prototype is public static List <TSource> ToList <TSource> (this IEnumerable <TSource> source ); located in System. linq. enumerable class
// The Enumerable class should be reflected, and this method is a public static method.
MethodInfo miToList = typeof (Enumerable). GetMethod ("ToList", BindingFlags. Static | BindingFlags. Public );
MiToList = miToList. MakeGenericMethod (typeEntity );
Args = new object [] {oq };
Object o = miToList. Invoke (null, args );
// Var q = ctx. CreateQuery <Department> (mmeESql. Text );
Repeated racesql. Text = oq. ToTraceString ();
GrdResultData. DataSource = o;
}

}
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.