Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 216 additions & 0 deletions src/System.Management.Automation/engine/CoreAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
Expand Down Expand Up @@ -5007,6 +5008,221 @@ private static XmlNode[] FindNodes(object obj, string propertyName, StringCompar
}
}

/// <summary>
/// Deals with DataRow objects
/// </summary>
internal class DataRowAdapter : PropertyOnlyAdapter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we follow the one class per file practice?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core adapters have always been in the same file. Some of them were moved to ExtraAdapter.cs previously due to missing the corresponding type in CoreCLR. Now we are gradually moving them back.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is more about the project in general.

{
#region virtual
/// <summary>
/// Retrieves all the properties available in the object.
/// </summary>
/// <param name="obj">object to get all the property information from</param>
/// <param name="members">collection where the members will be added</param>
protected override void DoAddAllProperties<T>(object obj, PSMemberInfoInternalCollection<T> members)
{
DataRow dataRow = (DataRow)obj;
if (dataRow.Table == null || dataRow.Table.Columns == null)
{
return;
}

foreach (DataColumn property in dataRow.Table.Columns)
{
members.Add(new PSProperty(property.ColumnName, this, obj, property.ColumnName) as T);
}

return;
}
/// <summary>
/// Returns null if propertyName is not a property in the adapter or
/// the corresponding PSProperty with its adapterData set to information
/// to be used when retrieving the property.
/// </summary>
/// <param name="obj">object to retrieve the PSProperty from</param>
/// <param name="propertyName">name of the property to be retrieved</param>
/// <returns>The PSProperty corresponding to propertyName from obj</returns>
protected override PSProperty DoGetProperty(object obj, string propertyName)
{
DataRow dataRow = (DataRow)obj;

if (!dataRow.Table.Columns.Contains(propertyName))
{
return null;
}

string columnName = dataRow.Table.Columns[propertyName].ColumnName;
return new PSProperty(columnName, this, obj, columnName);
}

/// <summary>
/// Returns the name of the type corresponding to the property
/// </summary>
/// <param name="property">PSProperty obtained in a previous DoGetProperty</param>
/// <param name="forDisplay">True if the result is for display purposes only</param>
/// <returns>the name of the type corresponding to the property</returns>
protected override string PropertyType(PSProperty property, bool forDisplay)
{
string columnName = (string)property.adapterData;
DataRow dataRow = (DataRow)property.baseObject;
var dataType = dataRow.Table.Columns[columnName].DataType;
return forDisplay ? ToStringCodeMethods.Type(dataType) : dataType.FullName;
}

/// <summary>
/// Returns true if the property is settable
/// </summary>
/// <param name="property">property to check</param>
/// <returns>true if the property is settable</returns>
protected override bool PropertyIsSettable(PSProperty property)
{
string columnName = (string)property.adapterData;
DataRow dataRow = (DataRow)property.baseObject;
return !dataRow.Table.Columns[columnName].ReadOnly;
}

/// <summary>
/// Returns true if the property is gettable
/// </summary>
/// <param name="property">property to check</param>
/// <returns>true if the property is gettable</returns>
protected override bool PropertyIsGettable(PSProperty property)
{
return true;
}


/// <summary>
/// Returns the value from a property coming from a previous call to DoGetProperty
/// </summary>
/// <param name="property">PSProperty coming from a previous call to DoGetProperty</param>
/// <returns>The value of the property</returns>
protected override object PropertyGet(PSProperty property)
{
DataRow dataRow = (DataRow)property.baseObject;
return dataRow[(string)property.adapterData];
}
/// <summary>
/// Sets the value of a property coming from a previous call to DoGetProperty
/// </summary>
/// <param name="property">PSProperty coming from a previous call to DoGetProperty</param>
/// <param name="setValue">value to set the property with</param>
/// <param name="convertIfPossible">instructs the adapter to convert before setting, if the adapter supports conversion</param>
protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible)
{
DataRow dataRow = (DataRow)property.baseObject;
dataRow[(string)property.adapterData] = setValue;
return;
}
#endregion virtual
}
/// <summary>
/// Deals with DataRowView objects
/// </summary>
internal class DataRowViewAdapter : PropertyOnlyAdapter
{
#region virtual
/// <summary>
/// Retrieves all the properties available in the object.
/// </summary>
/// <param name="obj">object to get all the property information from</param>
/// <param name="members">collection where the members will be added</param>
protected override void DoAddAllProperties<T>(object obj, PSMemberInfoInternalCollection<T> members)
{
DataRowView dataRowView = (DataRowView)obj;
if (dataRowView.Row == null || dataRowView.Row.Table == null || dataRowView.Row.Table.Columns == null)
{
return;
}

foreach (DataColumn property in dataRowView.Row.Table.Columns)
{
members.Add(new PSProperty(property.ColumnName, this, obj, property.ColumnName) as T);
}

return;
}
/// <summary>
/// Returns null if propertyName is not a property in the adapter or
/// the corresponding PSProperty with its adapterData set to information
/// to be used when retrieving the property.
/// </summary>
/// <param name="obj">object to retrieve the PSProperty from</param>
/// <param name="propertyName">name of the property to be retrieved</param>
/// <returns>The PSProperty corresponding to propertyName from obj</returns>
protected override PSProperty DoGetProperty(object obj, string propertyName)
{
DataRowView dataRowView = (DataRowView)obj;

if (!dataRowView.Row.Table.Columns.Contains(propertyName))
{
return null;
}
string columnName = dataRowView.Row.Table.Columns[propertyName].ColumnName;
return new PSProperty(columnName, this, obj, columnName);
}

/// <summary>
/// Returns the name of the type corresponding to the property
/// </summary>
/// <param name="property">PSProperty obtained in a previous DoGetProperty</param>
/// <param name="forDisplay">True if the result is for display purposes only</param>
/// <returns>the name of the type corresponding to the property</returns>
protected override string PropertyType(PSProperty property, bool forDisplay)
{
string columnName = (string)property.adapterData;
DataRowView dataRowView = (DataRowView)property.baseObject;
var dataType = dataRowView.Row.Table.Columns[columnName].DataType;
return forDisplay ? ToStringCodeMethods.Type(dataType) : dataType.FullName;
}

/// <summary>
/// Returns true if the property is settable
/// </summary>
/// <param name="property">property to check</param>
/// <returns>true if the property is settable</returns>
protected override bool PropertyIsSettable(PSProperty property)
{
string columnName = (string)property.adapterData;
DataRowView dataRowView = (DataRowView)property.baseObject;
return !dataRowView.Row.Table.Columns[columnName].ReadOnly;
}

/// <summary>
/// Returns true if the property is gettable
/// </summary>
/// <param name="property">property to check</param>
/// <returns>true if the property is gettable</returns>
protected override bool PropertyIsGettable(PSProperty property)
{
return true;
}

/// <summary>
/// Returns the value from a property coming from a previous call to DoGetProperty
/// </summary>
/// <param name="property">PSProperty coming from a previous call to DoGetProperty</param>
/// <returns>The value of the property</returns>
protected override object PropertyGet(PSProperty property)
{
DataRowView dataRowView = (DataRowView)property.baseObject;
return dataRowView[(string)property.adapterData];
}
/// <summary>
/// Sets the value of a property coming from a previous call to DoGetProperty
/// </summary>
/// <param name="property">PSProperty coming from a previous call to DoGetProperty</param>
/// <param name="setValue">value to set the property with</param>
/// <param name="convertIfPossible">instructs the adapter to convert before setting, if the adapter supports conversion</param>
protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible)
{
DataRowView dataRowView = (DataRowView)property.baseObject;
dataRowView[(string)property.adapterData] = setValue;
return;
}
#endregion virtual
}

internal class TypeInference
{
[TraceSource("ETS", "Extended Type System")]
Expand Down
Loading