PropertyDescriptor in GetDataSource

S

STech

I found the following pattern used a lot on webcontrols. Could you please
explain what the propDescs collection does? What confuses me is this:
new PropertyDescriptor[0]

Thanks.

<code>
ITypedList typedMemberList = memberList as ITypedList;
if (typedMemberList != null) {
PropertyDescriptorCollection propDescs =
typedMemberList.GetItemProperties(new PropertyDescriptor[0]);
PropertyDescriptor memberProperty = null;

if ((propDescs != null) && (propDescs.Count != 0)) {
string dataMember = DataMember;

if (dataMember.Length == 0) {
memberProperty = propDescs[0];
}
else {
memberProperty = propDescs.Find(dataMember, true);
}

if (memberProperty != null) {
object listRow = memberList[0];
object list = memberProperty.GetValue(listRow);

if (list is IEnumerable) {
return (IEnumerable)list;
}
}
</code>
 
S

Steven Cheng[MSFT]

Hi Stech,

Welcome to ASPNET newsgroup.
As for the PropertyDescriptor related pattern used in most
webcontrol(databound), here are some of my understandings:

First, the propertyDescriptor is just used to contain information to
represent a certain property in a class and can help to dynamically
retrieve propety values from a runtime class instance(something like
reflection). Well, let's look into the problem through the diassembled code
of ASP.NET DataGrid control's internal "GetResolvedDataSource" method:


======================================
internal static IEnumerable GetResolvedDataSource(object dataSource, string
dataMember)
{
if (dataSource != null)
{
IListSource source1 = dataSource as IListSource;
if (source1 != null)
{
IList list1 = source1.GetList();
if (!source1.ContainsListCollection)
{
return list1;
}
if ((list1 != null) && (list1 is ITypedList))
{
ITypedList list2 = (ITypedList) list1;
PropertyDescriptorCollection collection1 =
list2.GetItemProperties(new PropertyDescriptor[0]);
if ((collection1 == null) || (collection1.Count ==
0))
{
throw new
HttpException(HttpRuntime.FormatResourceString("ListSource_Without_DataMembe
rs"));
}
PropertyDescriptor descriptor1 = null;
if ((dataMember == null) || (dataMember.Length ==
0))
{
descriptor1 = collection1[0];
}
else
{
descriptor1 = collection1.Find(dataMember,
true);
}
if (descriptor1 != null)
{
object obj1 = list1[0];
object obj2 = descriptor1.GetValue(obj1);
if ((obj2 != null) && (obj2 is IEnumerable))
{
return (IEnumerable) obj2;
}
}
throw new
HttpException(HttpRuntime.FormatResourceString("ListSource_Missing_DataMembe
r", dataMember));
}
}
if (dataSource is IEnumerable)
{
return (IEnumerable) dataSource;
}
}
return null;
}
===========================

This function first check whether the control's assigned DataSource is an
IListSource object:
If it is not an IListSource, then check whether it is IEnumerable and
return the casted list/enumerable object.

If it is a IListDataSource, then because some DataSource are not direct
IList or IEnumerable object such as DataSet, so we have to further check
whether it is a IListSource's container or itself a IListSource (using the
ContainsListCollection). If it is a IListSource, we first use GetList()
function to rerieve the IList objects it contains.

For DataSet, ContainsListCollection will return true. And we convert the
retrieved List to a ITypeList for further use. In fact, the GetList() of
DataSet will return a DataViewManager, then when casting to ITypeList, we
can just the GetItemProperteis to find the proper bindable list/IEnumerable
object in it. You can look it further through the reflector tool. Anyway,
in a simple word, the ITypeList is used to dynamically find bindable list
collection from a List Container( such as dataset). Then, since all these
instances are of abstract type/interface, we need to use reflection to
retrieve that properties, and " PropertyDescriptor" class is just used
to query such information.

For detailed reference, I suggest you lookup the MSDN reference on all the
involved classes together with the disassemblied code in reflector. That'll
be much helper.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





--------------------
| Thread-Topic: PropertyDescriptor in GetDataSource
| thread-index: AcWNQL7jcGIli/TCTHyToe38sgOp9g==
| X-WBNR-Posting-Host: 128.194.92.153
| From: "=?Utf-8?B?U1RlY2g=?=" <[email protected]>
| Subject: PropertyDescriptor in GetDataSource
| Date: Wed, 20 Jul 2005 08:36:07 -0700
| Lines: 32
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10001
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| I found the following pattern used a lot on webcontrols. Could you please
| explain what the propDescs collection does? What confuses me is this:
| new PropertyDescriptor[0]
|
| Thanks.
|
| <code>
| ITypedList typedMemberList = memberList as ITypedList;
| if (typedMemberList != null) {
| PropertyDescriptorCollection propDescs =
| typedMemberList.GetItemProperties(new PropertyDescriptor[0]);
| PropertyDescriptor memberProperty = null;
|
| if ((propDescs != null) && (propDescs.Count != 0)) {
| string dataMember = DataMember;
|
| if (dataMember.Length == 0) {
| memberProperty = propDescs[0];
| }
| else {
| memberProperty = propDescs.Find(dataMember,
true);
| }
|
| if (memberProperty != null) {
| object listRow = memberList[0];
| object list =
memberProperty.GetValue(listRow);
|
| if (list is IEnumerable) {
| return (IEnumerable)list;
| }
| }
| </code>
|
 
S

Steven Cheng[MSFT]

Hi Stech,

Does my last reply helps or have you got any furthere ideas on this
question? If there're anything else
we can help, please feel free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 94148144
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Thu, 21 Jul 2005 05:30:38 GMT
| Subject: RE: PropertyDescriptor in GetDataSource
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Lines: 171
| Path: TK2MSFTNGXA01.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10015
| NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
|
| Hi Stech,
|
| Welcome to ASPNET newsgroup.
| As for the PropertyDescriptor related pattern used in most
| webcontrol(databound), here are some of my understandings:
|
| First, the propertyDescriptor is just used to contain information to
| represent a certain property in a class and can help to dynamically
| retrieve propety values from a runtime class instance(something like
| reflection). Well, let's look into the problem through the diassembled
code
| of ASP.NET DataGrid control's internal "GetResolvedDataSource" method:
|
|
| ======================================
| internal static IEnumerable GetResolvedDataSource(object dataSource,
string
| dataMember)
| {
| if (dataSource != null)
| {
| IListSource source1 = dataSource as IListSource;
| if (source1 != null)
| {
| IList list1 = source1.GetList();
| if (!source1.ContainsListCollection)
| {
| return list1;
| }
| if ((list1 != null) && (list1 is ITypedList))
| {
| ITypedList list2 = (ITypedList) list1;
| PropertyDescriptorCollection collection1 =
| list2.GetItemProperties(new PropertyDescriptor[0]);
| if ((collection1 == null) || (collection1.Count
==
| 0))
| {
| throw new
|
HttpException(HttpRuntime.FormatResourceString("ListSource_Without_DataMembe
| rs"));
| }
| PropertyDescriptor descriptor1 = null;
| if ((dataMember == null) || (dataMember.Length ==
| 0))
| {
| descriptor1 = collection1[0];
| }
| else
| {
| descriptor1 = collection1.Find(dataMember,
| true);
| }
| if (descriptor1 != null)
| {
| object obj1 = list1[0];
| object obj2 = descriptor1.GetValue(obj1);
| if ((obj2 != null) && (obj2 is IEnumerable))
| {
| return (IEnumerable) obj2;
| }
| }
| throw new
|
HttpException(HttpRuntime.FormatResourceString("ListSource_Missing_DataMembe
| r", dataMember));
| }
| }
| if (dataSource is IEnumerable)
| {
| return (IEnumerable) dataSource;
| }
| }
| return null;
| }
| ===========================
|
| This function first check whether the control's assigned DataSource is an

| IListSource object:
| If it is not an IListSource, then check whether it is IEnumerable and
| return the casted list/enumerable object.
|
| If it is a IListDataSource, then because some DataSource are not direct
| IList or IEnumerable object such as DataSet, so we have to further check
| whether it is a IListSource's container or itself a IListSource (using
the
| ContainsListCollection). If it is a IListSource, we first use GetList()
| function to rerieve the IList objects it contains.
|
| For DataSet, ContainsListCollection will return true. And we convert the
| retrieved List to a ITypeList for further use. In fact, the GetList() of
| DataSet will return a DataViewManager, then when casting to ITypeList, we
| can just the GetItemProperteis to find the proper bindable
list/IEnumerable
| object in it. You can look it further through the reflector tool. Anyway,
| in a simple word, the ITypeList is used to dynamically find bindable
list
| collection from a List Container( such as dataset). Then, since all these
| instances are of abstract type/interface, we need to use reflection to
| retrieve that properties, and " PropertyDescriptor" class is just used
| to query such information.
|
| For detailed reference, I suggest you lookup the MSDN reference on all
the
| involved classes together with the disassemblied code in reflector.
That'll
| be much helper.
|
| Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
|
|
|
|
| --------------------
| | Thread-Topic: PropertyDescriptor in GetDataSource
| | thread-index: AcWNQL7jcGIli/TCTHyToe38sgOp9g==
| | X-WBNR-Posting-Host: 128.194.92.153
| | From: "=?Utf-8?B?U1RlY2g=?=" <[email protected]>
| | Subject: PropertyDescriptor in GetDataSource
| | Date: Wed, 20 Jul 2005 08:36:07 -0700
| | Lines: 32
| | Message-ID: <[email protected]>
| | MIME-Version: 1.0
| | Content-Type: text/plain;
| | charset="Utf-8"
| | Content-Transfer-Encoding: 7bit
| | X-Newsreader: Microsoft CDO for Windows 2000
| | Content-Class: urn:content-classes:message
| | Importance: normal
| | Priority: normal
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
| | Xref: TK2MSFTNGXA01.phx.gbl
| microsoft.public.dotnet.framework.aspnet.webcontrols:10001
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| |
| | I found the following pattern used a lot on webcontrols. Could you
please
| | explain what the propDescs collection does? What confuses me is this:
| | new PropertyDescriptor[0]
| |
| | Thanks.
| |
| | <code>
| | ITypedList typedMemberList = memberList as ITypedList;
| | if (typedMemberList != null) {
| | PropertyDescriptorCollection propDescs =
| | typedMemberList.GetItemProperties(new PropertyDescriptor[0]);
| | PropertyDescriptor memberProperty = null;
| |
| | if ((propDescs != null) && (propDescs.Count != 0)) {
| | string dataMember = DataMember;
| |
| | if (dataMember.Length == 0) {
| | memberProperty = propDescs[0];
| | }
| | else {
| | memberProperty = propDescs.Find(dataMember,
| true);
| | }
| |
| | if (memberProperty != null) {
| | object listRow = memberList[0];
| | object list =
| memberProperty.GetValue(listRow);
| |
| | if (list is IEnumerable) {
| | return (IEnumerable)list;
| | }
| | }
| | </code>
| |
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top