Revit API에서 매개 변수를 가져 오려면 enum
을 보내서 가져 와서 원하는 유형으로 변환합니다. Revit API에서 요소에 특정 매개 변수가 있는지 확인합니다.
하지만 가끔은 매개 변수가 그래서 null
를 반환없는
null.AsDouble()
또는
null.AsValueString()
는
null exception
발생합니다.
나는 너무 많은 매개 변수를 검색하고 나는 삼항 연산자를 사용하고 있지만 문제는이
string typemark = e.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_MARK)!=null?wt.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_MARK).AsValueString():"";
double cost = e.get_Parameter(BuiltInParameter.ALL_MODEL_COST)!=null?wt.get_Parameter(BuiltInParameter.ALL_MODEL_COST).AsDouble():0;
내가 통과, 더 일반적인하는 기능을하고 생각하고 있었는데를 처리하는 가장 좋은 방법이라고 확신하지 않는다 매개 변수 및 결과를 변환 할 유형을 지정하십시오.
public static T CheckParameterAndFetch<T>(this Element e, BuiltInParameter p, Type t)
{
if (e.get_Parameter(p) != null)
{
if (t.GetType() == typeof(string))
{
return (T)Convert.ChangeType(e.get_Parameter(p).AsValueString(), typeof(string));
}
else if (t.GetType() == typeof(double))
{
return (T)Convert.ChangeType(e.get_Parameter(p).AsDouble(), typeof(double));
}
else if (t.GetType() == typeof(int))
{
return (T)Convert.ChangeType((int)e.get_Parameter(p).AsDouble(), typeof(double));
}
}
else
{
if (t.GetType() == typeof(string))
{
return (T)Convert.ChangeType("", typeof(string));
}
else if (t.GetType() == typeof(double))
{
return (T)Convert.ChangeType(0, typeof(double));
}
else if (t.GetType() == typeof(int))
{
return (T)Convert.ChangeType(0, typeof(int));
}
}
return (T)Convert.ChangeType(null, typeof(string));
}
하고 호출 할 때 나는 다음과 같은 입력 :
string typeName = wt.CheckParameterAndFetch(BuiltInParameter.ALL_MODEL_TYPE_NAME,typeof(string));
나는이 예외 얻을 : 방법
유형 인수를 'namespace.Extensions.CheckParameterAndFetch (Autodesk.Revit.DB .Element, Autodesk.Revit.DB.BuiltInParameter, System.Type) '을 (를) 사용에서 유추 할 수 없습니다. 형식 인수를 명시 적으로 지정하십시오. (CS0411)