本文整理汇总了C#中Class.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Class.Add方法的具体用法?C# Class.Add怎么用?C# Class.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Class
的用法示例。
在下文中一共展示了Class.Add方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetClass_ClassIsAddedAndRetrievedByName_SameClassIsReturned
public void GetClass_ClassIsAddedAndRetrievedByName_SameClassIsReturned()
{
Class c1 = new Class("Class1");
Class c2 = new Class("Class2");
Assert.IsNull(c1.GetClass("Class2"));
c1.Add(c2);
Assert.AreSame(c2, c1.GetClass("Class2"));
}
示例2: Main
private static void Main()
{
var cls = new Class();
Cache = new ObservableCollection<Student>(cls);
cls.CollectionChanged += ClassChanged;
cls.Add(new Student());
cls.Add(new Student());
cls[0].Name = "Peter";
cls[1].Name = "John";
cls.Move(0, 1);
cls.RemoveAt(1);
Dump(cls);
M.Changed += (_, args) => Console.WriteLine("Undo event: " + args.Event.GetType());
while (M.CanUndo) {
M.Undo();
Dump(cls);
}
}
示例3: Packing
public Packing(Type dpoType)
{
this.dpoType = dpoType;
instance = (PersistentObject)Activator.CreateInstance(this.dpoType);
this.publicFields = dpoType.GetFields(BindingFlags.Public | BindingFlags.Instance); //ignore public const fields
Type baseType = typeof(BasePackage<>);
baseType = baseType.MakeGenericType(dpoType);
this.classBuilder = new CSharpBuilder()
{
nameSpace = dpoType.Assembly.GetName().Name + "." + Setting.DPO_PACKAGE_SUB_NAMESPACE,
};
this.classBuilder.AddUsing("System")
.AddUsing("System.Data")
.AddUsing("System.Text")
.AddUsing("System.Collections.Generic")
.AddUsing("Sys")
.AddUsing("Sys.Data")
.AddUsing("Sys.Data.Manager")
.AddUsing(dpoType.Namespace);
var clss = new Class(ClassName, new CodeBuilder.TypeInfo { type = baseType })
{
modifier = Modifier.Public
};
//constructor
clss.Add(new Constructor(ClassName));
this.pack = new Method("Pack") { modifier = Modifier.Protected | Modifier.Override };
clss.Add(pack);
classBuilder.AddClass(clss);
}
示例4: CreateClass
protected override void CreateClass()
{
TableSchema schema = new TableSchema(tname);
Func<IColumn, string> COLUMN = column => "_" + column.ColumnName.ToUpper();
var clss = new Class(cname, OptionalBaseType()) { modifier = Modifier.Public | Modifier.Partial };
builder.AddClass(clss);
//Const Field
Field field;
foreach (var column in schema.Columns)
{
field = new Field(new TypeInfo { type = typeof(string) }, COLUMN(column), column.ColumnName)
{
modifier = Modifier.Public | Modifier.Const
};
clss.Add(field);
}
clss.AddCopyCloneEqualsFunc(cname, schema.Columns.Select(column => column.ColumnName));
}
示例5: CreateClass
protected override void CreateClass()
{
var clss = new Class(cname) { modifier = Modifier.Public | Modifier.Partial };
builder.AddClass(clss);
foreach (DataColumn column in dt.Columns)
{
clss.Add(new Property(dict[column], column.ColumnName) { modifier = Modifier.Public });
}
int i;
int count;
Statement sent;
clss = new Class(cname + "Extension") { modifier = Modifier.Public | Modifier.Static };
builder.AddClass(clss);
Func<DataColumn, string> COLUMN = column => "_" + column.ColumnName.ToUpper();
//Const Field
Field field;
foreach (DataColumn column in dt.Columns)
{
field = new Field(new TypeInfo { type = typeof(string) }, COLUMN(column), column.ColumnName)
{
modifier = Modifier.Public | Modifier.Const
};
clss.Add(field);
}
if (dt.TableName != null)
{
field = new Field(new TypeInfo { type = typeof(string) }, "TableName", dt.TableName)
{
modifier = Modifier.Public | Modifier.Const
};
clss.Add(field);
}
//primary keys
var pk = dt.Columns
.Cast<DataColumn>()
.Where(column => keys.Select(key => key.ToUpper()).Contains(column.ColumnName.ToUpper()))
.ToArray();
if (pk.Length == 0)
pk = new DataColumn[] { dt.Columns[0] };
string pks = string.Join(", ", pk.Select(key => COLUMN(key)));
field = new Field(new TypeInfo { type = typeof(string[]) }, "Keys")
{
modifier = Modifier.Public | Modifier.Static | Modifier.Readonly,
userValue = $"new string[] {LP} {pks} {RP}"
};
clss.Add(field);
Method method = new Method($"To{cname}Collection")
{
modifier = Modifier.Public | Modifier.Static,
type = new TypeInfo { userType = $"List<{cname}>" },
args = new Arguments().Add(typeof(DataTable), "dt"),
IsExtensionMethod = true
};
clss.Add(method);
sent = method.statements;
sent.AppendLine("return dt.AsEnumerable()");
sent.AppendLine(".Select(row => NewObject(row))");
sent.AppendLine(".ToList();");
{
Method method0 = new Method("NewObject")
{
modifier = Modifier.Public | Modifier.Static,
type = new TypeInfo { userType = cname },
args = new Arguments().Add(typeof(DataRow), "row"),
IsExtensionMethod = false
};
clss.Add(method0);
sent = method0.statements;
sent.AppendLine($"return new {cname}");
sent.Begin();
count = dt.Columns.Count;
i = 0;
foreach (DataColumn column in dt.Columns)
{
var type = dict[column];
var name = COLUMN(column);
var line = $"{column.ColumnName} = row.Field<{type}>({name})";
if (++i < count)
line += ",";
sent.AppendLine(line);
}
sent.End(";");
Method method1 = new Method("FillObject")
{
modifier = Modifier.Public | Modifier.Static,
//.........这里部分代码省略.........
示例6: CreateClass
protected override void CreateClass()
{
var clss = new Class(cname, new TypeInfo { type = typeof(IDataContractRow) }, new TypeInfo { userType = $"IEquatable<{cname}>" })
{
modifier = Modifier.Public | Modifier.Partial
};
builder.AddClass(clss);
foreach (DataColumn column in dt.Columns)
{
clss.Add(new Property(dict[column], column.ColumnName) { modifier = Modifier.Public });
}
int i;
int count;
Statement sent;
Func<DataColumn, string> COLUMN = column => "_" + column.ColumnName.ToUpper();
Method method;
Method mtdFillObject = new Method("FillObject")
{
modifier = Modifier.Public,
args = new Arguments().Add(typeof(DataRow), "row")
};
clss.Add(mtdFillObject);
Method mtdUpdateRow = new Method("UpdateRow")
{
modifier = Modifier.Public,
args = new Arguments().Add(typeof(DataRow), "row")
};
clss.Add(mtdUpdateRow);
Method mtdCopyTo = new Method("CopyTo")
{
modifier = Modifier.Public,
args = new Arguments().Add(cname, "obj")
};
clss.Add(mtdCopyTo);
var sent1 = mtdFillObject.statements;
var sent2 = mtdUpdateRow.statements;
var sent3 = mtdCopyTo.statements;
count = dt.Columns.Count;
i = 0;
foreach (DataColumn column in dt.Columns)
{
var type = dict[column];
var NAME = COLUMN(column);
var name = column.ColumnName;
var line = $"this.{name} = row.Field<{type}>({NAME});";
sent1.AppendLine(line);
line = $"row.SetField({NAME}, this.{name});";
sent2.AppendLine(line);
line = $"obj.{name} = this.{name};";
sent3.AppendLine(line);
}
Method mtdEquals = new Method("Equals")
{
modifier = Modifier.Public,
type = new TypeInfo { type = typeof(bool) },
args = new Arguments().Add(cname, "obj")
};
clss.Add(mtdEquals);
sent = mtdEquals.statements;
sent.AppendLine("return ");
var variables = dict.Keys.Select(column => column.ColumnName);
variables.ForEach(
variable => sent.Append($"this.{variable} == obj.{variable}"),
variable => sent.AppendLine("&& ")
);
sent.Append(";");
Method mtdNewObject = new Method("NewObject")
{
modifier = Modifier.Public | Modifier.Static,
type = new TypeInfo { userType = cname },
args = new Arguments().Add(typeof(DataRow), "row"),
IsExtensionMethod = false
};
clss.Add(mtdNewObject);
sent = mtdNewObject.statements;
sent.AppendLine($"return new {cname}");
sent.Begin();
count = dt.Columns.Count;
i = 0;
foreach (DataColumn column in dt.Columns)
{
var type = dict[column];
var NAME = COLUMN(column);
var line = $"{column.ColumnName} = row.Field<{type}>({NAME})";
if (++i < count)
//.........这里部分代码省略.........
示例7: ExportCSharpData
/// <summary>
/// create C# data from data table
/// </summary>
/// <param name="cmd"></param>
public void ExportCSharpData(Command cmd)
{
if (!(SqlShell.LastResult is DataTable))
{
stdio.ErrorFormat("display data table first by sql clause or command [type]");
return;
}
string ns = cmd.GetValue("ns") ?? "Sql.Data";
string cname = cmd.GetValue("class") ?? "Table";
var dt = SqlShell.LastResult as DataTable;
var builder = new CSharpBuilder { nameSpace = ns };
var clss = new Class(cname)
{
modifier = Modifier.Public | Modifier.Partial
};
builder.AddClass(clss);
Property prop;
foreach (DataColumn column in dt.Columns)
{
bool nullable = dt.AsEnumerable().Any(row => row[column] is DBNull);
TypeInfo ty = new TypeInfo(column.DataType) { Nullable = nullable };
prop = new Property(ty, column.ColumnName.ToFieldName()) { modifier = Modifier.Public};
clss.Add(prop);
}
clss = new Class(cname + "Data")
{
modifier = Modifier.Public
};
builder.AddClass(clss);
Func<int, string> tab = n => new string('\t', n);
string[] columns = dt.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray();
List<string> L = new List<string>();
foreach (DataRow row in dt.Rows)
{
List<string> V = new List<string>();
for (int i = 0; i < columns.Length; i++)
{
V.Add(string.Format("{0} = {1}", columns[i], VAL.Boxing(row[i]).ToString()));
}
L.Add($"{tab(3)}new {cname} {{ " + string.Join(", ", V) + " }");
}
var value = $"new {cname}[]\n" + $"{tab(2)}{{\n" + string.Join(",\n", L) + $"\n{tab(2)}}}";
Field field = new Field(new TypeInfo { userType = $"{cname}[]" }, "data")
{
modifier = Modifier.Public | Modifier.Static | Modifier.Readonly,
userValue = value
};
clss.Add(field);
string code = $"{builder}";
string path = cmd.GetValue("out");
if (path == null)
{
stdio.WriteLine(code);
}
else
{
string file = Path.ChangeExtension(Path.Combine(path, cname), "cs");
code.WriteIntoFile(file);
stdio.WriteLine("code generated on {0}", file);
}
}
示例8: CreateClass
protected override void CreateClass()
{
var clss = new Class(cname)
{
modifier = Modifier.Public | Modifier.Partial
};
clss.AddAttribute(new AttributeInfo("Table", new { Name = tname.ShortName }));
builder.AddClass(clss);
TableSchema schema = GetSchema(tname);
Property prop;
foreach (IColumn column in schema.Columns)
{
TypeInfo ty = new TypeInfo { userType = ColumnSchema.GetFieldType(column.DataType, column.Nullable) };
prop = new Property(ty, column.ToFieldName()) { modifier = Modifier.Public };
List<object> args = new List<object>();
args.Add(new { Name = column.ColumnName });
//args.Add(new { DbType = ColumnSchema.GetSQLType(column) + (column.Nullable ? " NULL" : " NOT NULL") });
if (column.IsPrimary)
args.Add(new { IsPrimaryKey = true });
if (column.IsIdentity)
args.Add(new { IsDbGenerated = true });
if (!column.IsPrimary && !column.Nullable)
args.Add(new { CanBeNull = false });
if (column.CType == CType.Text || column.CType == CType.NText)
args.Add(new AttributeInfoArg("UpdateCheck", "UpdateCheck.Never"));
prop.AddAttribute(new AttributeInfo("Column", args.ToArray()));
if (!column.IsComputed)
clss.Add(prop);
}
var fkBy = schema.ByForeignKeys.Keys.OrderBy(k => k.FK_Table);
Constructor constructor = null;
if (fkBy.Count() > 0)
{
clss.AppendLine();
constructor = new Constructor(this.cname);
}
List<Property> list = new List<Property>();
foreach (var key in fkBy)
{
prop = AddEntitySet(clss, constructor, key);
list.Add(prop);
}
var fks = schema.ForeignKeys;
//list = new List<Property>();
if (fks.Length > 0)
clss.AppendLine();
foreach (var key in fks.Keys)
{
prop = AddEntityRef(clss, key);
list.Add(prop);
}
if (constructor != null)
clss.Add(constructor);
foreach (var p in list)
clss.Add(p);
}
示例9: AddEntitySet
/// <summary>
/// add children tables
/// </summary>
/// <param name="clss"></param>
/// <param name="constructor"></param>
/// <param name="key"></param>
/// <returns></returns>
private Property AddEntitySet(Class clss, Constructor constructor, IForeignKey key)
{
TableName fk_tname = new TableName(tname.DatabaseName, key.FK_Schema, key.FK_Table);
string fk_cname = fk_tname.ToClassName(null);
string pname;
Property prop;
TypeInfo ty;
Field field;
var fk_schema = GetSchema(fk_tname);
var _keys = fk_schema.PrimaryKeys.Keys;
if (_keys.Length == 1 && _keys.Contains(key.FK_Column))
{
// 1:1 mapping
pname = clss.MakeUniqueName(Pluralization.Singularize(fk_cname));
ty = new TypeInfo { userType = $"EntityRef<{fk_cname}>" };
field = new Field(ty, $"_{pname}") { modifier = Modifier.Private };
prop = new Property(new TypeInfo { userType = fk_cname }, pname) { modifier = Modifier.Public };
prop.gets.Append($"return this._{pname}.Entity;");
prop.sets.Append($"this._{pname}.Entity = value;");
prop.AddAttribute(new AttributeInfo("Association",
new
{
Name = $"{this.cname}_{fk_cname}",
Storage = $"_{pname}",
ThisKey = key.PK_Column,
OtherKey = key.FK_Column,
IsUnique = true,
IsForeignKey = false
}));
}
else
{
//1:n mapping
pname = clss.MakeUniqueName(Pluralization.Pluralize(fk_cname));
constructor.statements.AppendLine($"this._{pname} = new EntitySet<{fk_cname}>();");
ty = new TypeInfo { userType = $"EntitySet<{fk_cname}>" };
field = new Field(ty, $"_{pname}") { modifier = Modifier.Private };
prop = new Property(ty, pname) { modifier = Modifier.Public };
prop.gets.Append($"return this._{pname};");
prop.sets.Append($"this._{pname}.Assign(value);");
prop.AddAttribute(new AttributeInfo("Association",
new
{
Name = $"{this.cname}_{fk_cname}",
Storage = $"_{pname}",
ThisKey = key.PK_Column,
OtherKey = key.FK_Column,
IsForeignKey = false
}));
}
clss.Add(field);
return prop;
}
示例10: AddEntityRef
/// <summary>
/// add foreighn keys
/// </summary>
/// <param name="clss"></param>
/// <param name="key"></param>
/// <returns></returns>
private Property AddEntityRef(Class clss, IForeignKey key)
{
string pk_cname = new TableName(tname.DatabaseName, key.PK_Schema, key.PK_Table).ToClassName(null);
string pname = clss.MakeUniqueName(pk_cname);
var field = new Field(new TypeInfo { userType = $"EntityRef<{pk_cname}>" }, $"_{pname}") { modifier = Modifier.Private };
clss.Add(field);
var prop = new Property(new TypeInfo { userType = pk_cname }, pname) { modifier = Modifier.Public };
prop.gets.Append($"return this._{pname}.Entity;");
prop.sets.Append($"this._{pname}.Entity = value;");
prop.AddAttribute(new AttributeInfo("Association",
new
{
Name = $"{pk_cname}_{this.cname}",
Storage = $"_{pname}",
ThisKey = key.FK_Column,
OtherKey = key.PK_Column,
IsForeignKey = true
}));
return prop;
}