我正在使用 VS 2010 (C#) T4 模板来生成代码。
我需要迭代项目中的所有类型,列出实体 poco 类并生成包装器。问题是,项目名称空间未被识别。
这是解决方案的结构:
namespace MySolution.Entities
{
public class Employee { ... }
public class Department { ... }
}
// Seperate project referenceing MySolution.Entities.
namespace MySolution.Database
{
public partial class Context { ... }
// Should generate Context.cs as a partial class with after iterating Syste.Types available in MySolution.Entities.
Context.tt
}
Run Code Online (Sandbox Code Playgroud)
这是文本模板:
<#@ template language="C#" #>
<#@ Output Extension=".cs" #>
namespace MySolution.Database
{
public partial class Context:
System.Data.Entity.DbContext
{
<#
System.Type [] types …Run Code Online (Sandbox Code Playgroud) 我一直在阅读Olin Shivers的一篇名为Stylish Lisp编程技术的文章,并发现那里的第二个例子(标有"Technique n-1")有点令人费解.它描述了一个自修改宏,如下所示:
(defun gen-counter macro (x)
(let ((ans (cadr x)))
(rplaca (cdr x)
(+ 1 ans))
ans))
Run Code Online (Sandbox Code Playgroud)
它应该将其调用形式作为参数x(即(gen-counter <some-number>)).这样做的目的是能够做到这样的事情:
> ;this prints out the numbers from 0 to 9.
(do ((n 0 (gen-counter 1)))
((= n 10) t)
(princ n))
0.1.2.3.4.5.6.7.8.9.T
>
Run Code Online (Sandbox Code Playgroud)
问题是这个带有macro函数名后面的符号的语法在Common Lisp中无效.我试图在Common Lisp中获得类似的行为是不成功的.有人可以提供CL中类似宏的工作示例吗?
免责声明:我发现很难在问题标题中概括问题,所以如果您有更好的建议,请在评论中告诉我。
让我们看一下以下简化的 TypeScript 类:
class Model {
save():Model {
// save the current instance and return it
}
}
Run Code Online (Sandbox Code Playgroud)
该类Model有一个save()返回自身实例的方法: a Model。
我们可以Model这样扩展:
class SomeModel extends Model {
// inherits the save() method
}
Run Code Online (Sandbox Code Playgroud)
因此,SomeModel将继承save(),但它仍然返回 a Model,而不是 a SomeModel。
有没有一种方法,也许使用泛型,将save()in的返回类型设置SomeModel为SomeModel,而不必在继承类内部重新定义它?
我的需求非常简单:我有一个提示表来接收评论并有评论也可以收到评论.
为了检索存储在同一个表(注释)中的每个注释,我为注释的注释创建了另一个键:"inverse_comments".
我试图通过使用自我引用关联使用一个注释表.有些资源似乎会带来不止一张表,这与我的需求不同.所以我想出了以下建模评论:
class Comment < ActiveRecord::Base
belongs_to :tip
belongs_to :user
has_many :mycomments,
:through => :inverse_comments,
:source => :comment
end
Run Code Online (Sandbox Code Playgroud)
显然这里缺少一些东西,但我无法弄明白.有人可以启发我这个:
我需要做些什么改变才能使模型工作?
谢谢.
我想问一下,我们将如何实现一个类的复制构造函数,该类具有自身指针作为其数据成员,我想实现深层复制,
class City
{
string name;
City* parent;
public:
City(string nam, double dcov);
City(string nam, double dcov, City* c);
City(const City& obj)
{
this-> name = obj.name;
// how to assign parent
parent = new City(??)
}
~City();
void setName(string name1);
void setDistanceCovered(int dist);
string getName();
double getDistanceCovered();
City* getParent(){return parent;}
};
Run Code Online (Sandbox Code Playgroud)
我很困惑,这行// how to assign parent
parent = new City(??)会再次调用构造函数而不是深层复制?问候.
当我尝试在构造函数中创建具有自引用的对象时,我得到StackOverflowError.
public class Example1 {
private int i;
private Example1 zero;
public Example1(int i) {
super();
if (i > 0) {
this.i = i;
} else {
this.i = this.zero.i;
}
this.zero = new Example1(i);
}
public int getI() {
return i;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用静态引用时,不会发生错误.
public class Example2 {
private int i;
private static final Example2 ZERO = new Example2(0);
public Example2() {
this(ZERO.i);
}
public Example2(int i) {
super();
this.i = i;
}
public int getI() {
return i;
} …Run Code Online (Sandbox Code Playgroud) 我有一个“问题”实体,可以将其保留到“问题”表中。一个问题可以有多种翻译。翻译只是另一种语言的另一个问题,与其带有parent_id字段的父问题相关联。因此,问题表具有 Question_id (int)、parent_id (int)、语言 (varchar)、prompt 等列。在我的设计中,parent_id 与翻译的 Question_id 相同。例如,假设我有一个 id 为 13 和 17 的英文问题(默认),而问题 13 有 3 个翻译:中文、日文和韩文。那么问题表将如下所示:
Question_id Parent_id 语言
13 13 英语;14 13 中文;15 13 日语;16 13 韩语;17 17 英语
在 Question 对象中,我想将问题与其翻译的关系映射为 @OneToMany 自引用关系。经过一些研究后,我通过以下方式实现了它:
@Entity
@Table(name = "question")
public class Question {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="parent_id")
private Question parent;
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
private Set<Question> translations;
Other properties...
Getters and setters....
}
Run Code Online (Sandbox Code Playgroud)
但是,由于它是自引用关系,我不理解以下内容:
您可以定义一个具有属于该类的成员字段的类,这似乎很奇怪。
public class Person
{
public string Name { get; set; }
public Person Spouse { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我调用该类:
var jane = new Person();
var jim = new Person();
jane.Spouse = jim;
jim.Spouse.Spouse = jane;
Run Code Online (Sandbox Code Playgroud)
最后一行会导致错误。但我在想象 jim.Spouse.Spouse 调用正在做什么时遇到了一些麻烦。它应该首先评估 jim.spouse ,它为空,所以它会导致错误,对吗?如果我首先断言 jim.Spouse = jane 那么它应该起作用吗?但我本质上是说 jane.spouse = jane.
如果一个类有一个相同类型的字段,那么它对该类做了什么?它只是允许类的两个实例建立关系吗?该类引用自身,因此它继承自自身?我有点困惑,也想知道这是否是不好的做法。
我有一个包含作业的"作业"表.事实上,乔布斯并不总是一气呵成......你可以找到一份有很多访问的工作.我打算将其表示为另一个工作,但通过自引用linkId链接回原始工作.
我无法使用流畅的API表示这一点.它是一对多关系..一个工作可能有很多访问,因此一些linkId指向原始工作.链接ID将返回到原始作业ID.它也是可选的,因为大多数工作可能一次性完成..
我已经找到了这个但很难将其他例子转换为这个例子,因为很多它们是一对一的,而且那些给出一对多例子的例子似乎是使用不同的EF6.
我的工作表是:
using System;
namespace JobsLedger.Model.Entities
{
public class Job : IEntityBase
{
public int Id { get; set; }
public string Model { get; set; }
public string Serial { get; set; }
public string ProblemDetails { get; set; }
public string SolutionDetails { get; set; }
public DateTime JobDate { get; set; }
public int BrandId { get; set; }
public int JobTypeId { get; set; }
public int StatusId { get; set; } …Run Code Online (Sandbox Code Playgroud) c# entity-framework self-reference entity-framework-core ef-fluent-api
My Core Data模型包含一个实体Shape,它有两个自引用关系,这意味着四个属性.一对是一对多关系(Shape.containedBy < - >> Shape.contains),另一对是多对多关系(Shape.nextShapes << - >> Shape.previousShapes).这一切都在应用程序中完美运行,因此我不认为自引用关系通常是一个问题.
但是,在将模型迁移到新版本时,Xcode无法编译自动生成的映射模型,并显示以下错误消息:
2009-10-30 17:10:09.387 mapc[18619:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "FUNCTION($manager ,'destinationInstancesForSourceRelationshipNamed:sourceInstances:' , 'contains' , $source.contains) == 1"'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff80d735a4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff83f0a313 objc_exception_throw + 45
2 Foundation 0x00007fff819bc8d4 _qfqp2_performParsing + 8412
3 Foundation 0x00007fff819ba79d +[NSPredicate predicateWithFormat:arguments:] + 59
4 Foundation 0x00007fff81a482ef +[NSExpression expressionWithFormat:arguments:] + 68
5 Foundation 0x00007fff81a48843 …Run Code Online (Sandbox Code Playgroud) cocoa core-data core-data-migration self-reference mapping-model
我正在尝试使用带有State模式的Swing库生成动态画布.我的代码可以编译,但是当按下按钮时,控制台上会出现红色标记.
问题:我的自引用指针或保留字这超出了按钮的范围.我希望能够在没有任何静态的情况下访问该类.
错误:线程"AWT-EventQueue-0"中的异常java.lang.Error:未解决的编译问题:类型State中的方法handlePush(SP)不适用于参数(new ActionListener(){})
这是画布类.
public class SP extends JPanel{
private static final long serialVersionUID = 1L;
private State state = null;
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowCanvasGUI();
}
});
}
public SP(State state ){
this.state = state;
init();
}
public SP(){
this(new BlackState());
}
public static void createAndShowCanvasGUI(){
JFrame frm = new JFrame("State Pattern");
frm.setVisible(true);
frm.setSize(400, 400);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setContentPane(new SP());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(state.getColor());
g.fillRect(0, 0, getWidth(), getHeight());
repaint(); …Run Code Online (Sandbox Code Playgroud) 这是一个简单的代码,我尝试使用自引用类型并同时使用别名.
#include <iostream>
class List {
private:
struct node {
int data;
struct node* next;
node(const int& d=0, struct node* n=nullptr) {
data = d; next = n;
}
~node() {};
};
using pNode = struct node*;
pNode head;
public:
List();
~List();
void print() const { std::cout << head->data; }
};
List::List() {
head = new node{55};
}
int main() {
List *a = new List;
a->print();
}
Run Code Online (Sandbox Code Playgroud)
以上工作正常.但是,我宁愿启动代码,如下所示:
class List {
private:
using pNode = struct node*;
struct node …Run Code Online (Sandbox Code Playgroud) 在运行时,汇编程序或机器代码(它是什么?)应该在RAM中的某个位置.我可以以某种方式访问它,读取甚至写入它吗?
这仅用于教育目的.
所以,我只能编译这段代码.我真的在这里读书吗?
#include <stdio.h>
#include <sys/mman.h>
int main() {
void *p = (void *)main;
mprotect(p, 4098, PROT_READ | PROT_WRITE | PROT_EXEC);
printf("Main: %p\n Content: %i", p, *(int *)(p+2));
unsigned int size = 16;
for (unsigned int i = 0; i < size; ++i) {
printf("%i ", *((int *)(p+i)) );
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我补充一下
*(int*)p =4;
Run Code Online (Sandbox Code Playgroud)
那是一个分段错误.
从答案中,我可以构造以下代码,在运行时修改它自己:
#include <stdio.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
void * alignptr(void * ptr, uintptr_t alignment) {
return (void *)((uintptr_t)ptr & ~(alignment …Run Code Online (Sandbox Code Playgroud) self-reference ×13
c# ×3
java ×3
c++ ×2
class ×2
static ×2
.net ×1
c ×1
c++11 ×1
cocoa ×1
common-lisp ×1
core-data ×1
deep-copy ×1
generics ×1
hibernate ×1
inheritance ×1
jpa ×1
linked-list ×1
lisp ×1
macros ×1
state ×1
swing ×1
t4 ×1
this ×1
this-pointer ×1
typescript ×1