csharp 反思prorperty类型和价值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 反思prorperty类型和价值相关的知识,希望对你有一定的参考价值。
static class ObjectExtensions
{
public static void SetPropertyValue<T>(this object obj, string propertyName, T propertyValue)
where T : IConvertible
{
PropertyInfo pi = obj.GetType().GetProperty( propertyName );
if( pi != null && pi.CanWrite )
{
pi.SetValue
(
obj,
Convert.ChangeType(propertyValue, pi.PropertyType),
null
);
}
}
}
class TestObject
{
public string Property1 { get; set; }
public int Property2 { get; set; }
}
void Main()
{
TestObject o = new TestObject();
// Propery1 == null, Property2 == 0
o.SetPropertyValue( "Property1", 1 );
o.SetPropertyValue( "Property2", "123" );
// Propery1 == 1, Property2 == 123
}
if (dataType.Equals(typeof(float)))
{
propertyValue = Convert.ToSingle(propertyValue);
}
if (dataType.Equals(typeof(double)))
{
propertyValue = Convert.ToDouble(propertyValue);
}
if (dataType.Equals(typeof(decimal)))
{
propertyValue = Convert.ToDecimal(propertyValue);
}
if (dataType.Equals(typeof(byte)))
{
propertyValue = Convert.ToByte(propertyValue);
}
if (dataType.Equals(typeof(string)))
{
propertyValue = Convert.ToString(propertyValue);
}
if (dataType.Equals(typeof(char)))
{
propertyValue = Convert.ToChar(propertyValue);
}
if (dataType.Equals(typeof(int)))
{
propertyValue = Convert.ToInt32(propertyValue);
}
if (dataType.Equals(typeof(long)))
{
propertyValue = Convert.ToInt64(propertyValue);
}
if (dataType.Equals(typeof(short)))
{
propertyValue = Convert.ToInt16(propertyValue);
}
public static object Parse(Type type, string str)
{
try
{
var parse = type.GetMethod("Parse", new[] {typeof(string)});
if (parse == null) throw new NotSupportedException();
return parse.Invoke(null, new object[] { str });
}
//or don't catch
catch (Exception)
{
return null;
}
}
propertyValue = Parse(dataType, propertyValue.ToString());
public async Task<IEnumerable<SettingDto>> PostFilter(SettingDto input)
{
//var data = await this.GetAll(new PagedResultRequestDto { MaxResultCount = 2, SkipCount = 7 });
var data = Mapper.Map<List<Setting>, List<SettingDto>>(await _settingRepository.GetAllListAsync());
var dataF = Filter<SettingDto>(data, input);
return dataF;
}
private List<T> Filter<T>(List<T> collection, T filterinput)
{
var filteredCollection = new List<T>();
var inputTypeProperties = filterinput.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var item in collection)
{
foreach (var property in inputTypeProperties)
{
Type CurrentPropertyType = property.PropertyType;
var filterValue = property.GetValue(filterinput, null);
//if (propValue != null)
// entity.GetType().GetProperty("Operation").SetValue(entity, "E");
var propertyInfo =
item.GetType()
.GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance);
if (propertyInfo == null)
throw new NotSupportedException("property given does not exists");
var propertyValue = propertyInfo.GetValue(item, null);
//var sss = ConvertValue < CurrentPropertyType >
//if (dataType.Equals(typeof(float)))
//{
// propertyValue = Convert.ToSingle(propertyValue);
//}
//if (dataType.Equals(typeof(double)))
//{
// propertyValue = Convert.ToDouble(propertyValue);
//}
//if (dataType.Equals(typeof(decimal)))
//{
// propertyValue = Convert.ToDecimal(propertyValue);
//}
//if (dataType.Equals(typeof(byte)))
//{
// propertyValue = Convert.ToByte(propertyValue);
//}
//if (dataType.Equals(typeof(string)))
//{
// propertyValue = Convert.ToString(propertyValue);
//}
//if (dataType.Equals(typeof(char)))
//{
// propertyValue = Convert.ToChar(propertyValue);
//}
//if (dataType.Equals(typeof(int)))
//{
// propertyValue = Convert.ToInt32(propertyValue);
//}
//if (dataType.Equals(typeof(long)))
//{
// propertyValue = Convert.ToInt64(propertyValue);
//}
//if (dataType.Equals(typeof(short)))
//{
// propertyValue = Convert.ToInt16(propertyValue);
//}
if (CurrentPropertyType.Equals(typeof(string)))
{
var convertedPropertyValue = Convert.ToString(propertyValue);
var convertedFilterValue = Convert.ToString(propertyValue);
if (convertedPropertyValue.Contains(convertedFilterValue,StringComparison.CurrentCultureIgnoreCase))
{
filteredCollection.Add(item);
break;
}
}
else
{
if (propertyValue.Equals(filterValue))
{
filteredCollection.Add(item);
break;
}
}
}
}
return filteredCollection;
}
public static T ConvertValue<T>(string value)
{
return (T)Convert.ChangeType(value, typeof(T));
}
public object Parse(Type type, string val)
{
try
{
var parse = type.GetMethod("Parse", new[] { typeof(string) });
if (parse == null) throw new NotSupportedException();
return parse.Invoke(null, new object[] { val });
}
//or don't catch
catch (Exception)
{
return null;
}
}
以上是关于csharp 反思prorperty类型和价值的主要内容,如果未能解决你的问题,请参考以下文章