本文整理汇总了C#中CodeWriter.GetCastExpression方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.GetCastExpression方法的具体用法?C# CodeWriter.GetCastExpression怎么用?C# CodeWriter.GetCastExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeWriter
的用法示例。
在下文中一共展示了CodeWriter.GetCastExpression方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteProcedureBodyReturnValue
protected virtual void WriteProcedureBodyReturnValue(CodeWriter writer, DbLinq.Schema.Dbml.Function procedure, string result, GenerationContext context)
{
Type returnType = GetProcedureType(procedure);
if (returnType != null)
writer.WriteLine(writer.GetReturnStatement(writer.GetCastExpression(writer.GetMemberExpression(result, "ReturnValue"), writer.GetLiteralType(returnType), true)));
}
示例2: WriteClassEqualsAndHash
protected virtual void WriteClassEqualsAndHash(CodeWriter writer, Table table, GenerationContext context)
{
List<DbLinq.Schema.Dbml.Column> primaryKeys = table.Type.Columns.Where(c => c.IsPrimaryKey).ToList();
if (primaryKeys.Count == 0)
{
writer.WriteLine("#warning L189 table {0} has no primary key. Multiple C# objects will refer to the same row.",
table.Name);
return;
}
using (writer.WriteRegion(string.Format("GetHashCode(), Equals() - uses column {0} to look up objects in liveObjectMap",
string.Join(", ", primaryKeys.Select(pk => pk.Member).ToList().ToArray()))))
{
// GetHashCode
using (writer.WriteMethod(SpecificationDefinition.Public | SpecificationDefinition.Override,
"GetHashCode", typeof(int)))
{
string hashCode = null;
foreach (var primaryKey in primaryKeys)
{
var member = writer.GetVariableExpression(primaryKey.Storage);
string primaryKeyHashCode = writer.GetMethodCallExpression(writer.GetMemberExpression(member, "GetHashCode"));
if (primaryKey.CanBeNull
|| GetType(primaryKey.Type, false).IsClass) // this patch to ensure that even if DB does not allow nulls,
// our in-memory object won't generate a fault
{
var isNullExpression = writer.GetEqualExpression(member, writer.GetNullExpression());
var nullExpression = writer.GetLiteralValue(0);
primaryKeyHashCode = "(" + writer.GetTernaryExpression(isNullExpression, nullExpression, primaryKeyHashCode) + ")";
}
if (string.IsNullOrEmpty(hashCode))
hashCode = primaryKeyHashCode;
else
hashCode = writer.GetXOrExpression(hashCode, primaryKeyHashCode);
}
writer.WriteLine(writer.GetReturnStatement(hashCode));
}
writer.WriteLine();
// Equals
string otherAsObject = "o";
using (writer.WriteMethod(SpecificationDefinition.Public | SpecificationDefinition.Override,
"Equals", typeof(bool), new ParameterDefinition { Type = typeof(object), Name = otherAsObject }))
{
string other = "other";
writer.WriteLine(writer.GetStatement(writer.GetAssignmentExpression(
writer.GetDeclarationExpression(other, table.Type.Name),
writer.GetCastExpression(otherAsObject, table.Type.Name,
false))));
using (writer.WriteIf(writer.GetEqualExpression(other, writer.GetNullExpression())))
{
writer.WriteLine(writer.GetReturnStatement(writer.GetLiteralValue(false)));
}
string andExpression = null;
foreach (var primaryKey in primaryKeys)
{
var member = writer.GetVariableExpression(primaryKey.Storage);
string primaryKeyTest = writer.GetMethodCallExpression(
writer.GetMemberExpression(
writer.GetMemberExpression(
writer.GetGenericName("System.Collections.Generic.EqualityComparer", primaryKey.Type),
"Default"),
"Equals"),
member,
writer.GetMemberExpression(other, member));
if (string.IsNullOrEmpty(andExpression))
andExpression = primaryKeyTest;
else
andExpression = writer.GetAndExpression(andExpression, primaryKeyTest);
}
writer.WriteLine(writer.GetReturnStatement(andExpression));
}
}
}
示例3: WriteProcedureBodyOutParameter
protected virtual void WriteProcedureBodyOutParameter(CodeWriter writer, DbLinq.Schema.Dbml.Parameter parameter, string result, int parameterIndex, GenerationContext context)
{
string p = writer.GetMethodCallExpression(writer.GetMemberExpression(result, "GetParameterValue"), parameterIndex.ToString());
string cp = writer.GetCastExpression(p, parameter.Type, true);
writer.WriteLine(writer.GetStatement(writer.GetAssignmentExpression(parameter.Name, cp)));
}