标签: system.reflection

有条件地在C#中实例化一个类

我一直在试图让新的代码工作.我已经能够通过if语句以更简单但更广泛的方式完成此操作.但是,由于这将是应用程序的第2版,我想让代码更清洁,更高效.

我想要做的是有条件地实例化一个类.它会是这样的:

int Airplane = 195;

if (Airplane == 190)
    _E190 Aircraft = new _E190();

else if (Airplane == 195)
    _E195 Aircraft = new _E195();
Run Code Online (Sandbox Code Playgroud)

我有两个类,但我想使用关键字"Aircraft",所以我不必为每个类创建多个代码部分.我还发现在C#中没有多重继承(在我看来,它会做到这一点),我对这种语言相对较新.

到目前为止,我已经完成了以我想要的方式实例化类,但我似乎无法使用每个类中声明的变量.我这样做了:

    int Airplane = 195;

    var Aircraft = Activator.CreateInstance( (Airplane == 195) ? typeof(_E195) : typeof(_E190));
    MessageBox.Show(Aircraft.GetType().ToString());
Run Code Online (Sandbox Code Playgroud)

MessageBox正确显示了类型,但同样,我无法使用"Aircraft"对象访问类本身.我确信有更多的方法可以做到这一点,但其中任何一个对我都没问题.

谢谢!

c# system.reflection

3
推荐指数
1
解决办法
1466
查看次数

尝试保存已发送的程序集时抛出NotSupportedException

http://pastebin.com/n6G1jTHQ
我的代码创建了一个新的程序集和模块,然后发出一个类SWT(在一个新程序集中),它应该与SWTTFields类完全相同.第137行,将程序集/模块保存到a中.dll,myAsmBuilder.Save("ModuleOne.dll");抛出异常:

mscorlib.dll中发生未处理的"System.NotSupportedException"类型异常附加信息:类型"SWT"未完成.

为什么我收到此错误?我的课程缺少什么?

.net c# cil reflection.emit system.reflection

3
推荐指数
1
解决办法
472
查看次数

在运行时动态更改对 dll 的引用

我遇到的情况是,不同的文件夹中有多个 .dll 文件,它们都具有相同的名称,包含相同的函数(具有相同的名称),但具有相同名称的函数内部的代码不同。

我在设计中创建了我的应用程序,引用了这些 .dll 文件之一。但我希望当我的应用程序启动时,使用选择案例能够更改对这些 dll 之一的引用。

这可能吗 ?

谢谢 !

c# dll assemblies system.reflection .net-assembly

3
推荐指数
1
解决办法
6418
查看次数

IsAssembly / IsFamily 和 IsFamilyOrAssembly 之间的关系

IsAssembly, IsPublic, IsFamily, IsFamilyOrAssembly, IsFamilyAndAssembly我已经阅读过相关内容,但我无法理解每个人的作用。这里奇怪的是IsFamily,在代码中返回并IsAssembly返回False,但IsFamilyOrAssembly返回True.

有人可以解释每个属性,因为我发现从文档中很难理解。当我开始阅读 c# 中的反射时,我遇到了所有这些。

public class Example
{
    public void m_public() {}
    internal void m_internal() {}
    protected void m_protected() {}
    protected internal void m_protected_public() {}

    public static void Main()
    {
        Console.WriteLine("\n{0,-30}{1,-18}{2}", "", "IsAssembly", "IsFamilyOrAssembly"); 
        Console.WriteLine("{0,-21}{1,-18}{2,-18}{3}\n", 
            "", "IsPublic", "IsFamily", "IsFamilyAndAssembly");

        foreach (MethodBase m in typeof(Example).GetMethods(
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
        {
            if (m.Name.Substring(0, 1) == "m")
            {
                Console.WriteLine("{0,-21}{1,-9}{2,-9}{3,-9}{4,-9}{5,-9}", 
                    m.Name,
                    m.IsPublic,
                    m.IsAssembly,
                    m.IsFamily,
                    m.IsFamilyOrAssembly,
                    m.IsFamilyAndAssembly
                );
            } …
Run Code Online (Sandbox Code Playgroud)

.net c# assemblies system.reflection .net-assembly

3
推荐指数
1
解决办法
270
查看次数

使用C#反射从类T获取属性列表

我需要从类属性的列表T- GetProperty<Foo>().我尝试了以下代码但它失败了.

样品类:

public class Foo {
    public int PropA { get; set; }
    public string PropB { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下代码:

public List<string> GetProperty<T>() where T : class {

    List<string> propList = new List<string>();

    // get all public static properties of MyClass type
    PropertyInfo[] propertyInfos;
    propertyInfos = typeof(T).GetProperties(BindingFlags.Public |
                                                    BindingFlags.Static);
    // sort properties by name
    Array.Sort(propertyInfos,
            delegate (PropertyInfo propertyInfo1,PropertyInfo propertyInfo2) { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });

    // write property names
    foreach (PropertyInfo propertyInfo in propertyInfos) {
        propList.Add(propertyInfo.Name); …
Run Code Online (Sandbox Code Playgroud)

c# properties system.reflection type-parameter

3
推荐指数
1
解决办法
1788
查看次数

Mono.Cecil - 如何获取方法体的简单示例

我一直在寻找一个新手问题,但找不到一个简单的例子。谁能给我一个简单的例子,如何将 MethodBody 放入最可用的字符串结果中?喜欢:

using Mono.Cecil;
using Mono.Cecil.Cil;

namespace my
{
    public class Main
    {
        public Main()
        {
             // phseudo code, but doesnt work
            Console.Write(    getMethod("HelloWorld").GetMethodBody().ToString()   );
        }

        public void HelloWorld(){
             MessageBox.Show("Hiiiiiiiiii");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# reflection system.reflection mono.cecil

3
推荐指数
1
解决办法
2388
查看次数

在 Windows 窗体中列出(顶级)声明的变量

创建表单的实例后,我可以轻松列出表单中的所有控件。

是否有任何机制可以列出所有声明的变量或此类对象?
也许我应该称之为声明。仅顶层声明就足够了。

假设我们有带有MyForm这样顶级声明的 Form:

Dim Town as String
Dim ZIP as String
Dim StreetName as String
Dim StreetNo as String
Public dtCountries as DataTable
Public LstCities as List(Of String)
...
Run Code Online (Sandbox Code Playgroud)

伪代码示例:

Dim MyForm as New MyForm          ' create instance of the form
Dim dtVariables as New Datatable  ' create a datatable to store found objects
dtVariables.Columns.Add("ID", GetTy(Int32))
dtVariables.Columns.Add("VariableName", GetTy(String))
dtVariables.Columns.Add("VariableType", GetTy(String))

For Each Varbl In MyForm.***variables***   ' <<< (how) to read all variables …
Run Code Online (Sandbox Code Playgroud)

.net vb.net reflection system.reflection winforms

3
推荐指数
1
解决办法
304
查看次数

为什么通过反射调用时,整数 '0' IEquatable.Equals 对 null 对象返回 true,而对整数调用时返回 false?

从整数 0 调用 IEquatable.Equals 来测试空对象应该返回 false,但使用反射时它返回 true。给定以下代码,我期望 foriEqualsequals返回“false”,但反射代码返回 true,为什么?我使用的是 C# 7.3 和 .NET Framework 4.6.2:

int zero = 0;
object nullObj = null;
var iEquals = zero.GetType().GetInterface("IEquatable`1").GetMethod("Equals");
var reflectionEquals = iEquals.Invoke(zero, new[] { nullObj }); // true
var equals = ((IEquatable<int>)zero).Equals(nullObj); // false
Run Code Online (Sandbox Code Playgroud)

.net c# reflection system.reflection

3
推荐指数
1
解决办法
103
查看次数

如何确定ParameterInfo是否为返回参数

如何确定a ParameterInfo是否为返回参数?

我写了下面的函数,但我担心我可能会遗漏一些东西:

public bool IsReturnParameter(ParameterInfo parameter){
    var method = parameter.Member as MethodInfo;
    return method != null && parameter.Equals(method.ReturnParameter);
}
Run Code Online (Sandbox Code Playgroud)

(1)参数都宣布对成员是:我的一对夫妇的假设,这可能是有缺陷的这个基础MethodInfo,ConstructorInfoPropertyInfo(索引).(2)ConstructorInfo并且PropertyInfo永远不会有返回参数.

.net c# reflection system.reflection parameterinfo

2
推荐指数
1
解决办法
380
查看次数

反射和自动生成的类型

我有一个类使用"yield"返回语句的单个方法.将自动创建嵌套类型.使用设置为绑定标志的反射BindingFlags.DeclaredOnly,我得到这个输出:

//我班上的公共成员
Test.FileSystemObject..ctor
Test.FileSystemObject.GetFiles(DirectoryInfo目录)
Test.FileSystemObject.GetFiles(String path)

// Auto generated nested class.  
Test.FileSystemObject+<GetFiles>d__4..ctor  
Test.FileSystemObject+<GetFiles>d__4.<>3__directory  
Test.FileSystemObject+<GetFiles>d__4.<>4__this  
Test.FileSystemObject+<GetFiles>d__4.<directories>5__7  
Test.FileSystemObject+<GetFiles>d__4.<files>5__8  
Test.FileSystemObject+<GetFiles>d__4.<FSO>5__6  
Test.FileSystemObject+<GetFiles>d__4.<i>5__9  
Test.FileSystemObject+<GetFiles>d__4.<unprocessed>5__5  
Test.FileSystemObject+<GetFiles>d__4.directory
Run Code Online (Sandbox Code Playgroud)

如何确定返回的类型是否assembly.GetTypes(BindingsFlags)是这样的自动生成类型?我正在寻找一种简单的方法来排除这些.

c# vb.net system.reflection yield-return

2
推荐指数
1
解决办法
792
查看次数