如何在“使用”中将动态类打开为静态定义部分?

发布于 2024-11-07 15:29:51 字数 927 浏览 1 评论 0原文

所以我想打开动态类,就像这样:

using System ...
...
using System.Windows.Forms;
using core = new myNamespace.myClass(); // Sure it doesn't work but how to realize it ?

当我

使用 myNamespace.myClass(); 时我无法使用它,因为 myClass 是动态的,但我希望它随着该类的创建和处理而创建。我想像从这个类一样访问 myClass 的变量/方法,比如

core.X 必须与 X 相同。

可能我只想要类似 Interface 的东西,但是作为一个类。

好吧,我会尝试准确解释我想要得到的内容:

例如,我有部分类,每个类都有 2 个文件!并且......第二个文件的内容是相同的。如何对所有部分类使用一个文件?就像一个模块但没有前缀!

所以我知道我的英语并不完美,我将添加一个代码示例:

public partial class FRIIB : Form
{
  private string x = username;
}

public partial class FRIIB : Form
{
  private string username = "hi";
}

public partial class LOLO : Form
{
  private string x = username;
  // I don't want to create another file with "username" definition 
}

我正在考虑使用 Mixin ...它看起来正是我想要得到的。

So I want to open dynamic class as something like that :

using System ...
...
using System.Windows.Forms;
using core = new myNamespace.myClass(); // Sure it doesn't work but how to realize it ?

when I do

using myNamespace.myClass(); I can't use it because myClass is dynamic but I want it creates as this class creates and being dispose with it. And I want to have access varables / methods of myClass aslike as from this class, like

core.X must be as same as X.

probably I just want something like Interface, but as a class.

Ok I will try to explain exactly what I want to get :

for example I've got partial classes, and each one with 2 files ! and ... the content of second file is the same. How do I use one file for all partial classes ? like a module but without prefix !

so I know my English isn't perfect , I will add a code example :

public partial class FRIIB : Form
{
  private string x = username;
}

public partial class FRIIB : Form
{
  private string username = "hi";
}

public partial class LOLO : Form
{
  private string x = username;
  // I don't want to create another file with "username" definition 
}

I was thinking about using Mixin ... it's looking like exactly what I want to get.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小红帽 2024-11-14 15:29:51

using 语句需要位于单个方法的上下文/范围内

如果您无法将范围限制为方法,则需要手动处置它,通常是在对象上实现 IDisposable,并在 Dispose() 中进行处置>。有时(但非常罕见),您可能还希望终结器在收集时捕获未处置的对象 - 但强调:非常罕见。

没有用于处理静态字段的内置机制 - 您必须添加静态Release()方法(或类似方法)以在干净的应用程序退出期间调用。

还有一个 using alias 看起来有点像你的代码,但做了一些完全不相关的事情(它允许你使用替代名称单一类型,无论是为了方便还是为了避免歧义;它仅限于单个文件)。

a using statement needs to be within the context/scope of a single method.

If you can't constrain the scope to a method, you'll need to dispose it manually, usually be implementing IDisposable on an object, and doing the dispose in Dispose(). Sometimes (but very rarely) you might also want a finalizer to catch undisposed objects as the get collected - but emphasis: very rare.

There is no inbuilt mechanism for disposing static fields - you'd have to add a static Release() method (or similar) to call during clean app-exit.

There is also a using alias which looks a bit like your code, but does something completely unrelated (it allows you to use an alternative name for a single type, either for convenience or to avoid ambiguity; it is limited to the single file).

初雪 2024-11-14 15:29:51

如果我正确理解您的问题,那么您正在寻找类似的内容:

object obj = Activator.CreateInstance(Assembly.LoadFrom("My Assembly").GetType("myNamespace.myClass", true));
core = obj as myNamespace.myClass;

第一个语句动态创建自定义类型的实例。第二条语句将创建的对象转换为您的自定义类型。

此代码片段仅显示正流,当然,我建议在您的代码中也注意负流。

我希望这有帮助:-)

If I understood your question correctly, then you're looking for something like this:

object obj = Activator.CreateInstance(Assembly.LoadFrom("My Assembly").GetType("myNamespace.myClass", true));
core = obj as myNamespace.myClass;

The first statement creates dynamically an instance of your custom type. The second statement casts the created object to your custom type.

This code snippet shows only positive flow, while, of course, I recommend in your code minding the negative flow as well.

I hope this helps :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文