本文整理汇总了C#中IModel.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IModel.GetType方法的具体用法?C# IModel.GetType怎么用?C# IModel.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IModel
的用法示例。
在下文中一共展示了IModel.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateAudit
private static string CreateAudit(IModel model, Controller controller, ActionResult result, IdentityService identity)
{
// set up the writer
StringBuilder stringBuilder = new StringBuilder();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = false };
XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings);
// start the document
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("audit");
// identity information
xmlWriter.WriteAttributeString("identity", identity.Identity);
// model information
xmlWriter.WriteAttributeString("model", model.GetType().Name);
// controller information
xmlWriter.WriteAttributeString("controller", controller.GetType().Name);
// result information
xmlWriter.WriteAttributeString("result", result.GetType().Name);
// end the document
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
return stringBuilder.ToString();
}
示例2: SaveModel
public static void SaveModel(this DbSet<IModel> modelSet, IModel model)
{
var modelName = model.GetType().Name;
ISavesModel<IModel> saver = (from s in ModelSavers
let t = s.GetType()
where t.GetGenericTypeDefinition() == typeof (ISavesModel<>)
from interfaces in t.GetGenericArguments().Select(ga => ga.GetInterfaces())
from i in interfaces
where i == model.GetType()
select s).FirstOrDefault();
if (saver == null)
throw new ModelSaverNotFoundException(string.Format("A {0} model saver could not be found.", modelName));
saver.SaveModel(model);
}
示例3: WriteModel
/// <summary>
/// Resolves and writes a model into the context.
/// </summary>
/// <param name="model">The model.</param>
public virtual void WriteModel(IModel model)
{
if (model == null)
return;
var translator = ResolveTranslator(model.GetType());
if (translator == null)
{
var syntacticModel = model as ISyntacticModel;
var hasSyntax = (syntacticModel != null);
throw new CompilationException("No translator was found to handle model of type "
+ model.GetType().FullName, model as ISyntacticModel);
}
translator.Translate(model, this);
}
示例4: typeof
bool IModel.AttributeEquals(IModel other)
{
if (other == null ||
other.GetType() != typeof(Person)) return false;
var person = (Person)other;
return
firstName == person.firstName
&& lastName == person.lastName
&& email == person.email
&& url == person.url;
}
示例5: ParseObject
/// <summary>Take an IModel and replace any properties it has that are other IModels with Id values</summary>
private dynamic ParseObject(IModel obj)
{
// if object is null, return null (covers casting issues)
if (obj == null) return null;
// create output as dictionary (will parse natively as a dynamic object)
var output = new Dictionary<string, object>();
// reflection isnt ideal here,prob okay for small data packets,
// but if it grows then it might start getting sluggish
// loop over the properties on the object
foreach (var property in obj.GetType().GetProperties())
{
var attributes = property.GetCustomAttributes(true);
foreach (var attr in attributes)
{
var hideValue = attr as HideValueAttribute;
if (hideValue != null)
{
// property value is hidden, so null its value move to next property
output.Add(property.Name, null);
goto NextProperty;
}
}
var value = property.GetValue(obj);
if (value is IModel)
{
// property is a single IModel, so grab its Id and output it in place of the object
output.Add(property.Name, (value as IModel).Id);
}
else if (value is IEnumerable<IModel>)
{
// property is collection of IModels, grab their ids and return as list of ints
output.Add(property.Name, (value as IEnumerable<IModel>).Select(v => v.Id).ToList());
}
else
{
// property isn't an IModel, so include it as it is
output.Add(property.Name, value);
}
NextProperty: continue;
}
return output;
}
示例6: Unresolve
/// <summary>
/// Set to null all link fields in the specified model.
/// </summary>
/// <param name="model">The model to look through for links</param>
public void Unresolve(IModel model)
{
foreach (IModel modelNode in Apsim.ChildrenRecursively(model))
{
// Go looking for private [Link]s
foreach (FieldInfo field in ReflectionUtilities.GetAllFields(
model.GetType(),
BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public))
{
LinkAttribute link = ReflectionUtilities.GetAttribute(field, typeof(LinkAttribute), false) as LinkAttribute;
if (link != null)
field.SetValue(model, null);
}
}
}
示例7: ResolveLinks
/// <summary>
/// Resolve all Link fields in the specified model.
/// </summary>
/// <param name="model">The model to look through for links</param>
/// <param name="linkTypeToMatch">If specified, only look for these types of links</param>
public static void ResolveLinks(IModel model, Type linkTypeToMatch = null)
{
string errorMsg = string.Empty;
// Go looking for [Link]s
foreach (FieldInfo field in ReflectionUtilities.GetAllFields(
model.GetType(),
BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public))
{
var link = ReflectionUtilities.GetAttribute(field, typeof(LinkAttribute), false) as LinkAttribute;
if (link != null &&
(linkTypeToMatch == null || field.FieldType == linkTypeToMatch))
{
object linkedObject = null;
List<IModel> allMatches = FindAll(model, field.FieldType);
// Special cases:
// if the type is an IFunction then must match on name and type.
// PMF organs have Live and Dead Biomass types. Need to link to correct one.
// Root1 has 'object NUptake3'. Need to use name to do link.
if (typeof(IFunction).IsAssignableFrom(field.FieldType) ||
typeof(IFunctionArray).IsAssignableFrom(field.FieldType) ||
typeof(Biomass).IsAssignableFrom(field.FieldType) ||
field.FieldType.Name == "Object")
{
linkedObject = allMatches.Find(m => m.Name == field.Name);
if (linkedObject == null)
allMatches.Clear();
}
else if (allMatches.Count >= 1)
linkedObject = allMatches[0]; // choose closest match.
if ((linkedObject == null) && (!link.IsOptional))
errorMsg = string.Format(": Found {0} matches for {1} {2} !", allMatches.Count, field.FieldType.FullName, field.Name);
if (linkedObject != null)
field.SetValue(model, linkedObject);
else if (!link.IsOptional)
throw new ApsimXException(
model,
"Cannot resolve [Link] '" + field.ToString() + errorMsg);
}
}
}
示例8: GetAllLinks
/// <summary>
/// Get all links. Useful for debugging.
/// </summary>
/// <returns></returns>
public static string GetAllLinks(IModel model)
{
string st = string.Empty;
st += "\r\n******" + Apsim.FullPath(model) + "******\r\n";
// Go looking for [Link]s
foreach (FieldInfo field in ReflectionUtilities.GetAllFields(
model.GetType(),
BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public))
{
var link = ReflectionUtilities.GetAttribute(field, typeof(LinkAttribute), false) as LinkAttribute;
if (link != null)
{
st += field.Name + " = ";
object value = field.GetValue(model);
if (value == null)
st += "null\r\n";
else if (value is IModel)
st += Apsim.FullPath(value as IModel) + "\r\n";
else
st += "??\r\n";
}
}
foreach (IModel child in model.Children)
st += GetAllLinks(child);
return st;
}
示例9: InsertObject
/// <summary>
/// 获取插入语法
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override int InsertObject(IModel obj)
{
Type type = obj.GetType();
string table = TypeCache.GetTableName(type, dbContext);
var typeArry = TypeCache.GetProperties(type, true).Values;
Attribute.FieldAttribute primaryKey = null;
string sql = string.Format("insert into [{0}](", table);
string sql1 = "";
string sql2 = "";
foreach (Attribute.FieldAttribute info in typeArry)
{
string name = info.Name;
if (info.IsPrimaryKey)
{
primaryKey = info;
}
if (info.IsPrimaryKey && !info.KeepIdentity)
{
continue;
}
if (!string.IsNullOrEmpty(info.VirtualField))
{
continue;
}
object value = info.GetValue(obj);
if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入
{
if (value == null)
{
continue;
}
}
value = ObjectConvert.SetNullValue(value, info.PropertyType);
sql1 += string.Format("{0},", info.KeyWordName);
sql2 += string.Format("@{0},", name);
helper.AddParam(name, value);
}
sql1 = sql1.Substring(0, sql1.Length - 1);
sql2 = sql2.Substring(0, sql2.Length - 1);
sql += sql1 + ") values( " + sql2 + ") ; ";
if (primaryKey.KeepIdentity)
{
sql += "SELECT " + primaryKey.GetValue(obj) + ";";
}
else
{
sql += "SELECT scope_identity() ;";
}
sql = SqlFormat(sql);
return Convert.ToInt32(helper.ExecScalar(sql));
}
示例10: GenerateModelArguments
/// <summary>
/// Creates a querystring parameter string from a model instance.
/// </summary>
/// <param name="model">Model to convert into a querystring.</param>
/// <returns>Query string representing the model provided.</returns>
public string GenerateModelArguments(IModel model)
{
if (model == null)
return null;
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("model", model.GetType().Name);
// For each model you want to support, you'll add any custom properties
// to the dictionary based on the type of object
if (model is ContentItemBase)
{
var item = model as ContentItemBase;
dic.Add("ID", item.ID);
}
else
{
return null;
}
// Create a querystring from the dictionary collection
return GeneralFunctions.CreateQuerystring(dic);
}
示例11: InsertObject
/// <summary>
/// 获取插入语法
/// </summary>
/// <param name="obj"></param>
/// <param name="helper"></param>
/// <returns></returns>
public override int InsertObject(IModel obj)
{
Type type = obj.GetType();
string table = TypeCache.GetTableName(type, dbContext);
var typeArry = TypeCache.GetProperties(type, true).Values;
string sql = string.Format("insert into {0}(", table);
string sql1 = "";
string sql2 = "";
string sequenceName = string.Format("{0}_sequence", table);
var sqlGetIndex = string.Format("select {0}.nextval from dual", sequenceName);//oracle不能同时执行多条语句
int id = Convert.ToInt32(helper.ExecScalar(sqlGetIndex));
foreach (Attribute.FieldAttribute info in typeArry)
{
string name = info.Name;
if (info.IsPrimaryKey && !info.KeepIdentity)
{
//continue;//手动插入ID
}
if (!string.IsNullOrEmpty(info.VirtualField))
{
continue;
}
object value = info.GetValue(obj);
if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入
{
if (value == null)
{
continue;
}
}
value = ObjectConvert.SetNullValue(value, info.PropertyType);
sql1 += string.Format("{0},", info.KeyWordName);
sql2 += string.Format("@{0},", info.KeyWordName);
helper.AddParam(info.KeyWordName, value);
}
sql1 = sql1.Substring(0, sql1.Length - 1);
sql2 = sql2.Substring(0, sql2.Length - 1);
sql += sql1 + ") values( " + sql2 + ")";
sql = SqlFormat(sql);
var primaryKey = TypeCache.GetTable(obj.GetType()).PrimaryKey;
helper.SetParam(primaryKey.Name, id);
helper.Execute(sql);
//var helper2 = helper as CoreHelper.OracleHelper;
//int id = helper2.Insert(sql,sequenceName);
return id;
}
示例12: FindEventField
/// <summary>
/// Locate and return the event backing field for the specified event. Returns
/// null if not found.
/// </summary>
/// <param name="eventName">The event publisher to find an event declaration for</param>
/// <param name="publisherModel">The model containing the event.</param>
/// <returns>The event field declaration</returns>
private static FieldInfo FindEventField(string eventName, IModel publisherModel)
{
Type t = publisherModel.GetType();
FieldInfo eventAsField = t.GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic);
while (eventAsField == null && t.BaseType != typeof(object))
{
t = t.BaseType;
eventAsField = t.GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic);
}
return eventAsField;
}
示例13: AddModelValidation
private static void AddModelValidation(this ValidationContext validationContext, IModel model, List<IModel> handledModels)
{
Argument.IsNotNull("validationContext", validationContext);
if (handledModels.Any(x => ReferenceEquals(x, model)))
{
return;
}
handledModels.Add(model);
validationContext.SynchronizeWithContext(model.ValidationContext, true);
var propertyDataManager = PropertyDataManager.Default;
var catelTypeInfo = propertyDataManager.GetCatelTypeInfo(model.GetType());
foreach (var property in catelTypeInfo.GetCatelProperties())
{
var propertyValue = model.GetValue(property.Key);
var enumerable = propertyValue as IEnumerable;
if (enumerable != null && !(propertyValue is string))
{
foreach (var item in enumerable)
{
var modelItem = item as IModel;
if (modelItem != null)
{
validationContext.AddModelValidation(modelItem, handledModels);
}
}
}
var propertyModel = propertyValue as IModel;
if (propertyModel != null)
{
validationContext.AddModelValidation(propertyModel, handledModels);
}
}
}
示例14: GetUnits
/// <summary>Gets the units from a declaraion.</summary>
/// <param name="model">The model containing the declaration field.</param>
/// <param name="fieldName">The declaration field name.</param>
/// <returns>The units (no brackets) or any empty string.</returns>
public static string GetUnits(IModel model, string fieldName)
{
FieldInfo field = model.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
if (field != null)
{
UnitsAttribute unitsAttribute = ReflectionUtilities.GetAttribute(field, typeof(UnitsAttribute), false) as UnitsAttribute;
if (unitsAttribute != null)
return unitsAttribute.ToString();
}
return string.Empty;
}
示例15: GetDescription
/// <summary>Gets the description from a declaraion.</summary>
/// <param name="model">The model containing the declaration field.</param>
/// <param name="fieldName">The declaration field name.</param>
/// <returns>The description or any empty string.</returns>
public static string GetDescription(IModel model, string fieldName)
{
FieldInfo field = model.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
if (field != null)
{
DescriptionAttribute descriptionAttribute = ReflectionUtilities.GetAttribute(field, typeof(DescriptionAttribute), false) as DescriptionAttribute;
if (descriptionAttribute != null)
return descriptionAttribute.ToString();
}
return string.Empty;
}