[/csharp][/csharp][/csharp][/csharp][/csharp]
Reflection
Get the names of all the properties of an object
#region License and Copyright
/*
* Dotnet Commons Reflection
*
* Copyright 2005. EDWARD LIM
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
*/
#endregion
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
//using Dotnet.Commons.Lang;
namespace Dotnet.Commons.Reflection
{
///
///
///
///
///
/// Copyright 2006 by Edward Lim.
/// All rights reserved.
///
///
public sealed class PropertyUtils
{
private PropertyUtils() {}
/// ——————————————————————–
///
///
/// the object to retreive the properties' names from
///
/// ——————————————————————–
public static string[] GetPropertyNames(object srcObj)
{
if (srcObj == null)
throw new System.ArgumentNullException(“srcObj”);
return GetPropertyNames(srcObj.GetType());
}
/// ——————————————————————–
///
///
/// the type to retreive the properties' names from
///
/// ——————————————————————–
public static string[] GetPropertyNames(Type objType)
{
if (objType == null)
throw new System.ArgumentNullException(“objType”);
string[] propertyNames = new string[objType.GetProperties().Length];
for (int i=0; i< objType.GetProperties().Length; i++) { propertyNames[i] = ((PropertyInfo)objType.GetProperties().GetValue(i)).Name; } return propertyNames; } } } [/csharp][/csharp][/csharp][/csharp][/csharp][/csharp][/csharp]
Copies a field value
[/csharp][/csharp][/csharp][/csharp][/csharp]
Get Property from Property path
/*
Copyright (c) 2010 James Craig
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Linq.Expressions;
using System.Collections;
#endregion
namespace Utilities
{
///
/// functions dealing with reflection.
///
public static class Reflection
{
public static PropertyInfo GetProperty
Gets a property's parent object
/*
Copyright (c) 2010 James Craig
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Linq.Expressions;
using System.Collections;
#endregion
namespace Utilities
{
///
/// functions dealing with reflection.
///
public static class Reflection
{
///
///
/// Source object
/// Path of the property (ex: Prop1.Prop2.Prop3 would be
/// the Prop1 of the source object, which then has a Prop2 on it, which in turn
/// has a Prop3 on it.)
/// Property info that is sent back
///
public static object GetPropertyParent(object SourceObject, string PropertyPath, out PropertyInfo PropertyInfo)
{
try
{
if (SourceObject == null)
{
PropertyInfo = null;
return null;
}
string[] Splitter = { “.” };
string[] SourceProperties = PropertyPath.Split(Splitter, StringSplitOptions.None);
object TempSourceProperty = SourceObject;
Type PropertyType = SourceObject.GetType();
PropertyInfo = PropertyType.GetProperty(SourceProperties[0]);
PropertyType = PropertyInfo.PropertyType;
for (int x = 1; x < SourceProperties.Length; ++x)
{
if (TempSourceProperty != null)
{
TempSourceProperty = PropertyInfo.GetValue(TempSourceProperty, null);
}
PropertyInfo = PropertyType.GetProperty(SourceProperties[x]);
PropertyType = PropertyInfo.PropertyType;
}
return TempSourceProperty;
}
catch { throw; }
}
}
}
[/csharp][/csharp][/csharp][/csharp][/csharp][/csharp][/csharp]
Gets a property's type
[/csharp][/csharp][/csharp][/csharp][/csharp]
Gets a property's value
/*
Copyright (c) 2010 James Craig
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Linq.Expressions;
using System.Collections;
#endregion
namespace Utilities
{
///
/// functions dealing with reflection.
///
public static class Reflection
{
///
///
/// object who contains the property
/// Path of the property (ex: Prop1.Prop2.Prop3 would be
/// the Prop1 of the source object, which then has a Prop2 on it, which in turn
/// has a Prop3 on it.)
///
/// be reached
public static object GetPropertyValue(object SourceObject, string PropertyPath)
{
try
{
if (SourceObject == null || string.IsNullOrEmpty(PropertyPath))
return null;
string[] Splitter = { “.” };
string[] SourceProperties = PropertyPath.Split(Splitter, StringSplitOptions.None);
object TempSourceProperty = SourceObject;
Type PropertyType = SourceObject.GetType();
for (int x = 0; x < SourceProperties.Length; ++x)
{
PropertyInfo SourcePropertyInfo = PropertyType.GetProperty(SourceProperties[x]);
if (SourcePropertyInfo == null)
return null;
TempSourceProperty = SourcePropertyInfo.GetValue(TempSourceProperty, null);
if (TempSourceProperty == null)
return null;
PropertyType = SourcePropertyInfo.PropertyType;
}
return TempSourceProperty;
}
catch { throw; }
}
}
}
[/csharp][/csharp][/csharp][/csharp][/csharp][/csharp][/csharp]