Reputation: 4025
I'm using Telerik RadGrid's NeedDataSource event to bind to a anonymous type.
Now OnItemDataBound is used to bind a DropDownList inside RadGrid.
protected void rgQuotations_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
dynamic di = e.Item.DataItem;
DropDownList ddlStatus = (e.Item.FindControl("ddlStatus") as DropDownList);
if (di.Status == 4)
{
ddlQuoteStatus.Items.Add("4");
}
}
}
When it tries to get value of di.Status
it throws exception
An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code
Additional information: 'object' does not contain a definition for 'Status'
But I can clearly see the value by hover on it.
How can I get this value without getting error?
Upvotes: 0
Views: 278
Reputation: 14591
As explained in C# ‘dynamic’ cannot access properties from anonymous types declared in another assembly, you can't use an anonymous object created in another assembly because it is internal.
As you are in control of the assembly where the object is instantiated, there is one possibe workaround (not mentioned in the linked answer, so I add the answer here). You can make the originating assembly internal types visible in the assembly which uses the object, by adding InternalsVisibleTo
assembly attribute:
// in assembly where you create the anonymous object
// and assuming the assembly where you use it in Grid is called Xyz
[assembly:InternalsVisibleTo("Xyz")]
Upvotes: 1