本文整理汇总了C#中CodeWriter.GetNewExpression方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.GetNewExpression方法的具体用法?C# CodeWriter.GetNewExpression怎么用?C# CodeWriter.GetNewExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeWriter
的用法示例。
在下文中一共展示了CodeWriter.GetNewExpression方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteClassHeader
public override void WriteClassHeader(CodeWriter writer, DbLinq.Schema.Dbml.Table table, GenerationContext context)
{
using (writer.WriteRegion(string.Format("{0} handling", typeof(INotifyPropertyChanging).Name)))
{
const string eventName = "PropertyChanging"; // do not change, part of INotifyPropertyChanging
const string emptyArgs = "emptyChangingEventArgs";
// event
writer.WriteEvent(SpecificationDefinition.Public, eventName, typeof(PropertyChangingEventHandler).Name);
writer.WriteLine();
// empty event arg
writer.WriteField(SpecificationDefinition.Private | SpecificationDefinition.Static,
writer.GetAssignmentExpression(emptyArgs, writer.GetNewExpression(
writer.GetMethodCallExpression(typeof(PropertyChangingEventArgs).Name, "\"\""))),
typeof(PropertyChangingEventArgs).Name);
// method
using (writer.WriteMethod(SpecificationDefinition.Protected | SpecificationDefinition.Virtual,
sendPropertyChangingMethod, null))
{
using (writer.WriteIf(writer.GetDifferentExpression(eventName, writer.GetNullExpression())))
{
writer.WriteLine(writer.GetStatement(writer.GetMethodCallExpression(eventName,
writer.GetThisExpression(), emptyArgs)));
}
}
}
}
示例2: WriteDataContextCtors
protected virtual void WriteDataContextCtors(CodeWriter writer, Database schema, Type contextBaseType, GenerationContext context)
{
// ctor taking a connections tring
WriteDataContextCtor(writer, schema, contextBaseType,
new[] { new ParameterDefinition { Name = "connectionString", Type = typeof(string) } },
new[] { "connectionString" },
new[] { typeof(string) },
context);
// the two constructors below have the same prototype, so they are mutually exclusive
// the base class requires a IVendor
if (!WriteDataContextCtor(writer, schema, contextBaseType,
new[] { new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) } },
new[] { "connection", writer.GetNewExpression(writer.GetMethodCallExpression(writer.GetLiteralFullType(context.SchemaLoader.Vendor.GetType()))) },
new[] { typeof(IDbConnection), typeof(IVendor) },
context))
{
// OR the base class requires no IVendor
WriteDataContextCtor(writer, schema, contextBaseType,
new[] { new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) } },
new[] { "connection" },
new[] { typeof(IDbConnection) },
context);
}
// ctor(string, MappingSource)
WriteDataContextCtor(writer, schema, contextBaseType,
new[] {
new ParameterDefinition { Name = "connection", Type = typeof(string) },
new ParameterDefinition { Name = "mappingSource", Type = typeof(MappingSource) },
},
new[] { "connection", "mappingSource" },
new[] { typeof(string), typeof (MappingSource) },
context);
// ctor(IDbConnection, MappingSource)
WriteDataContextCtor(writer, schema, contextBaseType,
new[] {
new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) },
new ParameterDefinition { Name = "mappingSource", Type = typeof(MappingSource) },
},
new[] { "connection", "mappingSource" },
new[] { typeof(IDbConnection), typeof(MappingSource) },
context);
// just in case you'd like to specify another vendor than the one who helped generating this file
WriteDataContextCtor(writer, schema, contextBaseType,
new[] {
new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) },
new ParameterDefinition { Name = "vendor", Type = typeof(IVendor) },
},
new[] { "connection", "vendor" },
new[] { typeof(IDbConnection), typeof(IVendor) },
context);
WriteDataContextCtor(writer, schema, contextBaseType,
new[] {
new ParameterDefinition { Name = "connection", Type = typeof(IDbConnection) },
new ParameterDefinition { Name = "mappingSource", Type = typeof(MappingSource) },
new ParameterDefinition { Name = "vendor", Type = typeof(IVendor) },
},
new[] { "connection", "mappingSource", "vendor" },
new[] { typeof(IDbConnection), typeof(MappingSource), typeof(IVendor) },
context);
}
示例3: WriteClassHeader
public override void WriteClassHeader(CodeWriter writer, DbLinq.Schema.Dbml.Table table, GenerationContext context)
{
using (writer.WriteRegion(string.Format("{0} handling", typeof(INotifyPropertyChanged).Name)))
{
const string eventName = "PropertyChanged"; // do not change, part of INotifyPropertyChanged
const string propertyNameName = "propertyName";
// event
writer.WriteEvent(SpecificationDefinition.Public, eventName, typeof(PropertyChangedEventHandler).Name);
writer.WriteLine();
// method
using (writer.WriteMethod(SpecificationDefinition.Protected | SpecificationDefinition.Virtual,
sendPropertyChangedMethod, null, new ParameterDefinition { Name = propertyNameName, Type = typeof(string) }))
{
using (writer.WriteIf(writer.GetDifferentExpression(eventName, writer.GetNullExpression())))
{
writer.WriteLine(writer.GetStatement(writer.GetMethodCallExpression(eventName,
writer.GetThisExpression(),
writer.GetNewExpression(writer.GetMethodCallExpression(typeof(PropertyChangedEventArgs).Name,
propertyNameName)))));
}
}
}
}
示例4: WriteClassCtor
/// <summary>
/// Writes class ctor.
/// EntitySet initializations
/// </summary>
/// <param name="writer"></param>
/// <param name="table"></param>
/// <param name="schema"></param>
/// <param name="context"></param>
protected virtual void WriteClassCtor(CodeWriter writer, Table table, Database schema, GenerationContext context)
{
using (writer.WriteRegion("ctor"))
using (writer.WriteCtor(SpecificationDefinition.Public, table.Type.Name, new ParameterDefinition[0], null))
{
// children are EntitySet
foreach (var child in GetClassChildren(table))
{
// if the association has a storage, we use it. Otherwise, we use the property name
var entitySetMember = child.Storage ?? child.Member;
writer.WriteLine(writer.GetStatement(
writer.GetAssignmentExpression(
entitySetMember,
writer.GetNewExpression(writer.GetMethodCallExpression(
writer.GetGenericName(TypeExtensions.GetShortName(typeof(EntitySet<>)), child.Type),
GetChildAttachMethodName(child),
GetChildDetachMethodName(child)
))
)
));
}
// the parents are the entities referenced by a FK. So a "parent" is an EntityRef
foreach (var parent in GetClassParents(table))
{
var entityRefMember = parent.Storage;
writer.WriteLine(writer.GetStatement(
writer.GetAssignmentExpression(
entityRefMember,
writer.GetNewExpression(writer.GetMethodCallExpression(
writer.GetGenericName(TypeExtensions.GetShortName(typeof(EntityRef<>)), parent.Type)
))
)
));
}
writer.WriteLine(writer.GetStatement(writer.GetMethodCallExpression("OnCreated")));
}
}
示例5: WriteClassPropertyAccessorSet
/// <summary>
/// Writes property setter, for FK properties
/// </summary>
/// <param name="writer"></param>
/// <param name="property"></param>
/// <param name="relatedAssociations"></param>
/// <param name="context"></param>
private void WriteClassPropertyAccessorSet(CodeWriter writer, Column property, IEnumerable<Association> relatedAssociations, GenerationContext context)
{
// if new value if different from old one
using (writer.WriteIf(writer.GetDifferentExpression(writer.GetPropertySetValueExpression(),
writer.GetVariableExpression(property.Storage))))
{
// if the property is used as a FK, we ensure that it hasn't been already loaded or assigned
foreach (var relatedAssociation in relatedAssociations)
{
// first thing to check: ensure the association backend isn't already affected.
// if it is the case, then the property must not be manually set
// R# considers the following as an error, but the csc doesn't
//var memberName = ReflectionUtility.GetMemberInfo<DbLinq.Data.Linq.EntityRef<object>>(e => e.HasLoadedOrAssignedValue).Name;
var memberName = "HasLoadedOrAssignedValue";
using (writer.WriteIf(writer.GetMemberExpression(relatedAssociation.Storage, memberName)))
{
writer.WriteLine(writer.GetThrowStatement(writer.GetNewExpression(
writer.GetMethodCallExpression(
writer.GetLiteralFullType(
typeof(
System.Data.Linq.
ForeignKeyReferenceAlreadyHasValueException
)))
)));
}
}
// the before and after are used by extensions related to interfaces
// for example INotifyPropertyChanged
// here the code before the change
foreach (IImplementation implementation in context.Implementations())
implementation.WritePropertyBeforeSet(writer, property, context);
// property assignment
writer.WriteLine(
writer.GetStatement(
writer.GetAssignmentExpression(writer.GetVariableExpression(property.Storage),
writer.GetPropertySetValueExpression())));
// here the code after change
foreach (IImplementation implementation in context.Implementations())
implementation.WritePropertyAfterSet(writer, property, context);
}
}