有没有办法检索有关.NET API的元数据?
例如,假设我想获得为其定义的所有属性的列表System.Windows.Documents.List.以一些结构化格式(如XML,JSON等)获取此信息会很好.每个条目应如下所示:
<property name="MarkerStyle" type="TextMarkerStyle" get="true" set="true"/>
Run Code Online (Sandbox Code Playgroud)
我想避免屏幕刮掉MSDN库.:-)
我有一个如下所示的方法......
public bool MakeRequest(string[] args)
{
try
{
sXmlRequest = args[0];
sResponse = "";
Console.WriteLine(sXmlRequest);
sw.Write(sXmlRequest);
sw.Flush();
sResponse = sr.ReadToEnd();
return true;
}
catch (Exception e)
{
sResponse = e.Message;
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我必须使用Reflection调用此方法,因为整个框架的设置方式.
这是我用来调用它的代码
string[] innerargs = {"Dummy Args"};
string pAssembly = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\TCPConnector.dll";
Assembly assemblyInstance = Assembly.LoadFrom(pAssembly);
Type tConnector = assemblyInstance.GetType("Fit.TcpConnector");
Object oLateBound = assemblyInstance.CreateInstance(tConnector.FullName);
result = tConnector.InvokeMember("MakeRequest", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, oLateBound, innerargs);
Run Code Online (Sandbox Code Playgroud)
这将返回MissingMethodException,表示找不到方法Fit.TcpConnector.MakeRequest.
但是,如果我将MakeRequest的签名更改为
public bool MakeRequest(string args)
Run Code Online (Sandbox Code Playgroud)
代替
public bool MakeRequest(string[] …Run Code Online (Sandbox Code Playgroud) 可以说我有以下课程:
public class Provider
{
...
public sealed class Slice
{
public readonly double firstName;
public readonly double secondName;
public readonly double thirdName;
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
此类用于保存滑动时间序列,包含的Slice类是返回值.(Provider.Last属性返回Slice的最新实例).
我需要通过属性的名称获取最新返回的Slice类的属性值.
PropertyInfo secondNameProperty = Provider.Last.GetType().GetProperty("secondName");
double secondNameValue = (double)secondNameProperty.GetValue(Provider.Last, null);
Run Code Online (Sandbox Code Playgroud)
GetProperty返回null.我怎样才能做到这一点?
我正在尝试检测我们的应用程序是否从DVD运行(因为这会禁用/启用逻辑中的功能).到目前为止,我已经提出了下面的代码片段似乎可行,但我真的想知道是否有最佳实践来检测这一点.
public static bool IsDVDInstallation()
{
try
{
string location = Assembly.GetExecutingAssembly().Location;
var info = new DriveInfo(Path.GetPathRoot(location));
return info.DriveType == DriveType.CDRom;
}
catch
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 我已经用const字符串创建了一类类(显示其中一个),我想要实现它.
public static class HTDB_Cols
{
public sealed class Assistant
{
public const string EntryID = "entryID",
CustName = "custName",
SerialNum = "serialNum",
UserName = "userName",
Password = "password",
EndDate = "end_date",
CustID = "custID",
TmpCheck = "tmpCheck",
Isfamily = "isfamily",
Isserver = "isserver";
}
}
public static class DB
{
public static void insert(string TableName)
{
ColumnsCollection = typeof(HTDB_Cols).GetNestedTypes().Where(f => f.DeclaringType.Name.ToLower().Equals(TableName.ToLower()));
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码显示了我的尝试,但即使经过大量的试验和错误,我仍然无法做到正确.
我希望将所有列的列表作为const集合数组或列表.
有没有办法用变量引用属性名称?
场景:对象A具有公共整数属性X和Z,所以......
public void setProperty(int index, int value)
{
string property = "";
if (index == 1)
{
// set the property X with 'value'
property = "X";
}
else
{
// set the property Z with 'value'
property = "Z";
}
A.{property} = value;
}
Run Code Online (Sandbox Code Playgroud)
这是一个愚蠢的例子,所以请相信,我有一个用途.
是否有函数或使用反射方法来获取所有系统类型.
像那些: - System.Int64
System.Byte[]
System.Boolean
System.String
System.Decimal
System.Double
...
我们有一个旧的enum存储一些数据类型.我们需要将它们转换为.net types.
我试图从dll实例化一个对象而不引用它.我可以使用VB.NET中的Reflection来实现它,但无法弄清楚如何在C#中实现它.
在VB.NET中:
Public Class Form1
Dim bytes() As Byte = System.IO.File.ReadAllBytes("\\path\directory\file.dll")
Dim assmb As System.Reflection.Assembly = System.Reflection.Assembly.Load(bytes)
Dim myDllClass As Object = assmb.CreateInstance("myNamespace.myClass")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim conStr As String = myDllClass.publicConString
Dim dt As DataTable = myDllClass.MethodReturnsDatatable("select * from Part", conStr)
DataGridView1.DataSource = dt
End Sub
Run Code Online (Sandbox Code Playgroud)
在C#中:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static byte[] bytes = System.IO.File.ReadAllBytes(@"\\path\directory\file.dll");
static System.Reflection.Assembly assmb = System.Reflection.Assembly.Load(bytes);
object myDllClass = …Run Code Online (Sandbox Code Playgroud) 我想调用这个方法:
public IEnumerable<TEntity> SelectAll(params string[] entitiesToLoad)
Run Code Online (Sandbox Code Playgroud)
但是在大多数情况下我不会加载关系实体,所以我想调用它而不发送entitiesToLoad值,如下所示:
var dbResult = methodInfo.Invoke(repository, null);
Run Code Online (Sandbox Code Playgroud)
但它抛出异常,因为参数的数量不相等
有没有办法在不将params string []更改为其他类型的参数的情况下执行此操作?
我试过string entitiesToLoad = null as parammeter,我得到了同样的错误.
我需要能够访问.NET Core中类的当前属性的名称.
public class MyClass
{
private string _myProperty;
public string MyProperty
{
get => _myProperty;
set
{
// GOAL: Check my property name and then do something with that information.
// The following used to works in the full .NET Framework but not .NET Core.
// var propName = System.Reflection.MethodBase.GetCurrentMethod().Name;
_myProperty = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在完整的.NET Framework中,我们可以使用如下的反射.
System.Reflection.MethodBase.GetCurrentMethod().Name;
Run Code Online (Sandbox Code Playgroud)
(我们记得,一个房产实际上只是一种方法.)
但是,它似乎没有在.NET Core 1.1中提供此功能 - 尽管它(可能/将)在.NET Standard 2.0中可用.
在这个GitHub问题(https://github.com/dotnet/corefx/issues/12496)上,有一个GetMethodInfoOf用于访问属性名称的引用.但是,我无法找到此方法的任何示例,并且指向可能示例的GitHub链接似乎已断开.
可以使用哪种其他策略或技术来检索当前属性的名称?
非常感谢!
c# ×10
.net ×4
reflection ×3
.net-core ×1
arrays ×1
asp.net-4.0 ×1
dll ×1
driveinfo ×1
fieldinfo ×1
parameters ×1
vb.net ×1