本文整理汇总了C#中CodeWriter.GetDeclarationExpression方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.GetDeclarationExpression方法的具体用法?C# CodeWriter.GetDeclarationExpression怎么用?C# CodeWriter.GetDeclarationExpression使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeWriter
的用法示例。
在下文中一共展示了CodeWriter.GetDeclarationExpression方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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));
}
}
}