我不认为这是可能的,但由于我没有明确的MSDN清晰度,我觉得最好问一下.假设我们有一个类如下.
public partial class Hazaa
{
public int Shazoo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,我想将Shazoo归为SuperCool, 但我希望在另一个文件中这样做.由于我使用的是分部分类,我可以按如下方式添加新属性.
public partial class Hazaa
{
[SuperCool]
public int Wheee { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是我可以通过在后者中编写代码来归因第一个样本中声明的属性吗?我怀疑这是可能的,但我很乐意经过纠正.如果是这样,语法是什么?
我使用部分类来分割2个文件之间的一些功能,但我收到一个错误.我究竟做错了什么?
A1.cs:
private partial class A
{
private string SomeProperty { get { return "SomeGeneratedString"; } }
}
Run Code Online (Sandbox Code Playgroud)
A2.cs:
private partial class A
{
void SomeFunction()
{
//trying to access this.SomeProperty produces the following compiler error, at least with C# 2.0
//error CS0117: 'A' does not contain a definition for 'SomeProperty'
}
}
Run Code Online (Sandbox Code Playgroud) 模板类方法是否有部分特化?
template <class A, class B>
class C
{
void foo();
}
Run Code Online (Sandbox Code Playgroud)
它不像这样专门化它:
template <class A> void C<A, CObject>::foo() {};
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
我正在使用EF Core DbFirst方法。我的dbContext是由Scaffold-DbContext命令自动创建的。
我需要将其他DbSet添加到dbContext中,并向dbContext的OnModelCreating方法中添加一些其他代码,但是在删除每个添加的代码的脚手架之后,我必须再次添加它。
我想要做的是创建另一个局部dbContext类,并将受保护的覆盖标记无效OnModelCreating(ModelBuilder modelBuilder)作为局部方法。
但出现错误:
这是一个伪代码:
MyDbContext1.cs-由Scaffold-DbContext生成
public partial class MyDbContext : DbContext
{
public MyDbContext()
{
}
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public virtual DbSet<Client> Clients { get; set; }
protected override partial void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Client>(entity =>
{
// ... some code
}
}
}
Run Code Online (Sandbox Code Playgroud)
MyDbContext2.cs-我每次在脚手架上添加到dbContext中的这段代码:
public partial class MyDbContext
{
public virtual DbSet<JustAnotherEntity> AnotherEntity { get; set; }
protected override partial void OnModelCreating(ModelBuilder modelBuilder)
{ …Run Code Online (Sandbox Code Playgroud) c# entity-framework partial-classes partial entity-framework-core
我已经为我的xsd自动生成的类创建了一个部分类.问题在于调试这个部分类.无法识别断点,或者编译器不会在分部类中设置的断点处中断.
// Autogenerated class by xsd.exe
public partial class Class1
{
private Class1Brand[] brandField;
private string Class1guidField;
.....
}
// Debug Part - probably in a different file
public partial class Class1
{
public static Validity setValidity(Validity validity)
{
// ********* BREAKPOINT IS SET ON THE NEXT LINE ***********
validity.LastVerified = DateTime.Now;
//certificates are only updated within 14 days before expiry date
TimeSpan tsCheck = validity.NotAfter - validity.LastVerified;
if (tsCheck.Days <= 14)
{
DateTime dtNotBefore = validity.NotAfter.AddDays(conf.validityPeriod());
if (validity.NotAfter > …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的局部课
public partial class ABC
{
public string GetName()
{
//some code here
}
public string GetAge()
{
//some code here
}
}
public partial class ABC
{
public string GetSex()
{
//some code here
}
public string GetAge()
{
//some code here
}
}
Run Code Online (Sandbox Code Playgroud)
这两个类在构建时如何合并?请给我解释一下.
public partial class Form1 : Form
Run Code Online (Sandbox Code Playgroud)
本声明中的部分内容是什么意思?我知道我们有一个继承自Form的Form1类.但部分意味着什么呢?
考虑一个实现大量接口的类,使用partial class定义在单独的文件中实现每个接口是否有意义?
这是滥用语言功能还是我不知道的成语?
EF已经为我生成了一些部分类,每个都有一个构造函数,但它表示不要触摸它们(下面的示例),现在如果我创建自己的辅助部分类并且我想要一个构造函数自动设置一些字段如何我会这样做,因为它会发生冲突吗?
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Breakdown.Models
{
using System;
using System.Collections.Generic;
public partial class Call
{
public Call()
{
this.Logs = new HashSet<Log>();
}
...
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正处于一个必须使用部分类的场景中.在这个部分类中,我有一些方法需要解决另一个类中的字段.
例如
编辑:对不起:第一堂课已经宣布了partial!
public partial class myClass
{
private string _myString;
public string myString
{
get { return _myString; }
set { _myString = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
和
public partial class myClass
{
public void doSomething()
{
myString = "newString";
}
}
Run Code Online (Sandbox Code Playgroud)
编译器说myString部分类中不存在!
我怎样才能克服这个问题?
partial-classes ×10
c# ×8
.net ×3
attributes ×1
breakpoints ×1
c++ ×1
constructor ×1
debugging ×1
interface ×1
partial ×1
syntax ×1
templates ×1