/*-------------------------------------------------------------------------
* 功能描述:ExtensibleStorageUtil
* 作者:xulisong
* 创建时间: 2019/7/1 9:39:17
* 版本号:v1.0
* -------------------------------------------------------------------------*/
using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB.ExtensibleStorage;
namespace FWindSoft.Revit.ExtensibleStorage
{
///
/// 扩展存储工具类
///
public class ExtensibleStorageUtil
{
///
/// 扩展数据内容兼容类型
///
private static HashSet ContentTypes { get; set; } = new HashSet();
///
/// 扩展数据键值兼容类型
///
private static HashSet KeyTypes { get; set; } = new HashSet();
static ExtensibleStorageUtil()
{
InitData();
}
private static void InitData()
{
#region 内容类型
ContentTypes.Add(typeof(bool));
ContentTypes.Add(typeof(byte));
ContentTypes.Add(typeof(Int16));
ContentTypes.Add(typeof(Int32));
ContentTypes.Add(typeof(float));
ContentTypes.Add(typeof(double));
ContentTypes.Add(typeof(ElementId));
ContentTypes.Add(typeof(Guid));
ContentTypes.Add(typeof(string));
ContentTypes.Add(typeof(XYZ));
ContentTypes.Add(typeof(UV));
ContentTypes.Add(typeof(Entity));
#endregion
#region 键值类型
KeyTypes.Add(typeof(bool));
KeyTypes.Add(typeof(byte));
KeyTypes.Add(typeof(Int16));
KeyTypes.Add(typeof(Int32));
KeyTypes.Add(typeof(ElementId));
KeyTypes.Add(typeof(Guid));
KeyTypes.Add(typeof(string));
#endregion
}
///
/// 判定是否是有效的content类型
///
///
///
public static bool IsEffectiveContent(Type type)
{
return ContentTypes.Contains(type);
}
///
/// 判定是否是有效的key类型
///
///
///
public static bool IsEffectiveKey(Type type)
{
return KeyTypes.Contains(type);
}
public static bool IsEffectiveArray(Type type,out Type contentType)
{
contentType = null;
var baseType = type.GetInterface(typeof(IList<>).FullName);
if (baseType == null)
{
return false;
}
var firstType = baseType.GetGenericArguments()?.FirstOrDefault();
var flag= firstType != null && IsEffectiveContent(firstType);
if (flag)
{
contentType = firstType;
}
return flag;
}
public static bool IsEffectiveMap(Type type,out Type outKeyType,out Type outValueTYpe)
{
outKeyType = null;
outValueTYpe = null;
var baseType = type.GetInterface(typeof(IDictionary<,>).FullName);
if (baseType == null)
{
return false;
}
var keyType = baseType.GetGenericArguments()?.FirstOrDefault();
var valueType = baseType.GetGenericArguments()?.FirstOrDefault();
var flag= keyType != null && valueType != null && IsEffectiveKey(keyType) && IsEffectiveContent(valueType);
if (flag)
{
outKeyType = keyType;
outValueTYpe = valueType;
}
return flag;
}
///
/// 是否为自定义解析存储
///
///
///
public static bool IsCoustomStorage(Type type)
{
var baseType = type.GetInterface(typeof(ICoustomStorage).FullName);
return baseType != null;
}
///
/// 获取可用set方法
///
///
public static MethodInfo GetEntitySetMethod()
{
var methods= typeof(Entity).GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach (var methodInfo in methods)
{
if (methodInfo.Name != "Set")
{
continue;
}
var parameters = methodInfo.GetParameters();
if (parameters.Length == 2 && parameters[0].ParameterType == typeof(string))
{
return methodInfo;
}
}
return null;
}
public static Type GetFileType(Field field)
{
var useType = field.ValueType;
if (field.ContainerType == ContainerType.Array)
{
useType = typeof(IList<>).MakeGenericType(field.ValueType);
}
else if (field.ContainerType == ContainerType.Map)
{
useType = typeof(IDictionary<,>).MakeGenericType(field.KeyType, field.ValueType);
}
return useType;
}
}
}