我一直在试图让新的代码工作.我已经能够通过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"对象访问类本身.我确信有更多的方法可以做到这一点,但其中任何一个对我都没问题.
谢谢!
http://pastebin.com/n6G1jTHQ
我的代码创建了一个新的程序集和模块,然后发出一个类SWT(在一个新程序集中),它应该与SWTTFields类完全相同.第137行,将程序集/模块保存到a中.dll,myAsmBuilder.Save("ModuleOne.dll");抛出异常:
mscorlib.dll中发生未处理的"System.NotSupportedException"类型异常附加信息:类型"SWT"未完成.
为什么我收到此错误?我的课程缺少什么?
我遇到的情况是,不同的文件夹中有多个 .dll 文件,它们都具有相同的名称,包含相同的函数(具有相同的名称),但具有相同名称的函数内部的代码不同。
我在设计中创建了我的应用程序,引用了这些 .dll 文件之一。但我希望当我的应用程序启动时,使用选择案例能够更改对这些 dll 之一的引用。
这可能吗 ?
谢谢 !
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) 我需要从类属性的列表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) 我一直在寻找一个新手问题,但找不到一个简单的例子。谁能给我一个简单的例子,如何将 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) 创建表单的实例后,我可以轻松列出表单中的所有控件。
是否有任何机制可以列出所有声明的变量或此类对象?
也许我应该称之为声明。仅顶层声明就足够了。
假设我们有带有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) 从整数 0 调用 IEquatable.Equals 来测试空对象应该返回 false,但使用反射时它返回 true。给定以下代码,我期望 foriEquals和equals返回“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) 如何确定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,ConstructorInfo或PropertyInfo(索引).(2)ConstructorInfo并且PropertyInfo永远不会有返回参数.
我有一个类使用"yield"返回语句的单个方法.将自动创建嵌套类型.使用设置为绑定标志的反射BindingFlags.DeclaredOnly,我得到这个输出:
//我班上的公共成员
Test.FileSystemObject..ctor
Test.FileSystemObject.GetFiles(DirectoryInfo目录)
Test.FileSystemObject.GetFiles(String path)Run Code Online (Sandbox Code Playgroud)// 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
如何确定返回的类型是否assembly.GetTypes(BindingsFlags)是这样的自动生成类型?我正在寻找一种简单的方法来排除这些.
c# ×9
.net ×5
reflection ×4
assemblies ×2
vb.net ×2
cil ×1
dll ×1
mono.cecil ×1
properties ×1
winforms ×1
yield-return ×1