本文介绍了将Convert.ChangeType用于泛型和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我当时正在处理一个问题,可能是因为我的设计不好,但是我找到了解决方案,我想知道是否有人认为它有问题或总体上是一个更好的解决方案.

Hello, i was dealing with a problem, probly because a bad design on my part but i find a solution and i want to know if anybody sees a problem with it or a better solution in general.

我对ActiveRecord数据访问模式进行了简单的实现,用ActiveRecordAttribute装饰了实体,并且有一个ActiveRecord< T>.类,该类接收修饰的类的实例(实体),并使用来自属性的信息 可以生成CRUD操作和其他内容的sql.无论如何,重要的是:

I made a simple implementation of the ActiveRecord data acces pattern, entitys are decorated with a ActiveRecordAttribute and there is an ActiveRecord<T> class that receives an instance of the decorated class (the entity) and using the info from attributes can generate the sql of CRUD operations and other stuff. Anyway the important is:

public class ActiveRecord<T> : IDisposable where T : new()
{
	public T Instance { get; protected set; }

	public ActiveRecord(T instance)
	{
		Instace = instance;
	}

	// lot of more stuff
}


要使用此功能,我总是不得不做类似的事情:

To use this i always had to do something like:

Entity e = new Entity();

using(ActiveRecrd<Entity> are = new ActiveRecord<Entity>(e)
{
	// do stuff with are
	are.Fetch();
	//...
	are.Update();
	//...

}

但是现在我想做些更实际的事情,而又不失去这种行为,我想这样做:

But now i want to do something more practical, without loosing this behavior, i want to do this:

public class Entity : ActiveRecord<Entity>
{
  // Entity inheriths methods of ActiveRecord<Entity>
}

因此,在构造了一些受保护的构造函数之后,我的问题是如何使用具有该属性的同一对象(此对象)设置Instance属性,当然,对于每个Entity类,简单的路径应该可以做到这一点(在ActiveRecord中启用空的构造函数):

So after make some protected constructor my proble was how to set Instance property with the same object that has it (this), of course the easy path is for each Entity class this should do it fine (enabling an empty constructor in ActiveRecord):

public Entity()
{
	Instance = this;
}

但是这种情况是由其他人造成的,他们可能会忘记设置它并造成问题,所以我真的很想将构造函数设置为实例,所以我首先尝试了

But this clases are made by others and they can forget to set this and make problems so i really want to have a constructor wich force Instance setted to this so i tried first:

public Entity()
	:base(this) // Keyword 'this' is not available in the current context
{
}

理解这是不好的,我在没有参数的ActiveRecord的新受保护构造函数中尝试了这些事情:

Understanding this was no good i tried this things in a new protected constructor of ActiveRecord with no arguments:

protected ActiveRecord()
{
	// Error Cannot implicit convert ActiveRecrd<T> to T
	Instance = this;

    // Error Cannot convert type ActiveRecrd<T> to T
	Instance = (T)this;

}

最终可以正常工作

protected ActiveRecord()
{
	Instance = (T)Convert.ChangeType(this, typeof(T));
}

我不确定这是否真的可以,任何人都知道它是如何工作的(以及我尝试做的事情:)

Im not sure if it is all ok really, anyone understand how it works (and what i tried to do :)

现在我想我可以安全地执行类似的代码了:

Now i think i can safely do code like:

// public class Entity : ActiveRecord<Entity>

Entity e = new Entity();
e.Id = 1;
e.Fetch();
e.Description = "Updated by ActiveRecord";
e.Update();

这将被广泛使用,因此,如果您发现此问题,请告诉我.

谢谢

-

推荐答案


这篇关于将Convert.ChangeType用于泛型和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:50