- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我在 stackoverflow 中的第一个问题,我是使用反射的初学者。
我想转储对象实例的所有值以供引用(以跟踪测试中使用的值)。我使用的是 Compact Framework 3.5 而不是完整的框架。请记住这一点以提出您的建议。
想象以下类:
public class Camera : IDisposable
{
public Camera.FilenameProperties SnapshotFile;
public double DigitalZoomFactor { get; set; }
public bool DisplayHistogram { get; set; }
public int ImageUpdateInterval { get; set; }
public Camera.ImprintCaptionPosType ImprintCaptionPos { get; set; }
public string ImprintCaptionString { get; set; }
}
“特殊”类型是:
public class FilenameProperties
{
public string Directory { get; set; }
public string Filename { get; set; }
public Camera.FilenamePaddingType FilenamePadding { get; set; }
public Camera.ImageType ImageFormatType { get; set; }
public Camera.ImageResolutionType ImageResolution { get; set; }
public int JPGQuality { get; set; }
public void Restore();
public void Save();
public enum Fnametype
{
tSnapshot = 0,
tCircularCapture = 1,
}
}
public enum ImprintCaptionPosType
{
Disabled = 0,
LowerRight = 1,
LowerLeft = 2,
LowerCenter = 3,
UpperRight = 4,
UpperLeft = 5,
UpperCenter = 6,
Center = 7,
}
现在,我可以获得相机实例的“基本”名称和属性以及字段名称:
Camera cam = new Camera();
dumpProperties(cam);
...
void dumpProperties(object oClass)
{
System.Diagnostics.Debug.WriteLine(oClass.ToString());
FieldInfo[] _Info = oClass.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
for(int i = 0; i<_Info.Length; i++)
{
System.Diagnostics.Debug.WriteLine(_Info[i].Name + ":'" + _Info[i].GetValue(oClass).ToString()+"'");
}
foreach (PropertyInfo pi in oClass.GetType().GetProperties())
{
System.Diagnostics.Debug.WriteLine(pi.Name + ":'" + pi.GetValue(oClass, null)
+ "' Type=" + pi.PropertyType.ToString());
}
}
然后得到这样的东西:
Intermec.Multimedia.Camera
SnapshotFile:'Intermec.Multimedia.Camera+FilenameProperties'
DigitalZoomFactor:'1' Type=System.Double
DisplayHistogram:'False' Type=System.Boolean
ImageUpdateInterval:'1' Type=System.Int32
ImprintCaptionPos:'Disabled' Type=Intermec.Multimedia.Camera+ImprintCaptionPosType
ImprintCaptionString:'' Type=System.String
现在,对于像 DigitalZoomFactor 和 ImageUpdateInterval 这样的简单属性,我得到了我需要的,但是对于嵌套类(正确的措辞?)我只得到了类型,例如 SnapshotFile。对于嵌套枚举,我得到与“ImprintCaptionPos”一样的值。
如何获取嵌套值的值,例如 SnapshotFile 字段/属性的 FilenameProperties.Filename?
如果我使用 dumpProperties(cam.SnapshotFile),我会得到我正在寻找的输出:
Intermec.Multimedia.Camera+FilenameProperties
Directory:'\Program Files\FrmCamera' Type=System.String
Filename:'myphoto' Type=System.String
ImageFormatType:'JPG' Type=Intermec.Multimedia.Camera+ImageType
FilenamePadding:'None' Type=Intermec.Multimedia.Camera+FilenamePaddingType
ImageResolution:'Medium' Type=Intermec.Multimedia.Camera+ImageResolutionType
JPGQuality:'100' Type=System.Int32
但是我怎样才能使它自动化呢?
我进行了大量搜索和测试编码,但无法找到解决方案。问题似乎是让字段的实例能够遍历它。
我没有 Camera 类的源代码,所以我无法在其中添加或删除代码。
有人能帮忙吗?
我需要得到类似调试器显示的东西:
最佳答案
你只需要使用递归,如果你的属性是一个类,就循环回到方法中。下面是我们使用的 XML 序列化例程的示例,它使用反射有效地遍历目标的属性,并从中生成一个 XElement
。您的逻辑会有所不同,因为您不打算构建 XML,但您将要做的事情的结构非常相似。
public XElement Serialize(object source,
string objectName,
bool includeNonPublicProperties)
{
XElement element;
var flags = BindingFlags.Instance | BindingFlags.Public;
if(includeNonPublicProperties)
{
flags |= BindingFlags.NonPublic;
}
var props = source.GetType().GetProperties(flags);
var type = source.GetType();
string nodeName;
if(objectName == null)
{
if (type.IsGenericType)
{
nodeName = type.Name.CropAtLast('`');
}
else
{
nodeName = type.Name;
}
}
else
{
nodeName = objectName;
}
element = new XElement(nodeName);
foreach (var prop in props)
{
string name = prop.Name;
string value = null;
bool valIsElement = false;
if (!prop.CanRead) continue;
if(prop.PropertyType.IsEnum)
{
value = prop.GetValue(source, null).ToString();
}
else
{
string typeName;
if (prop.PropertyType.IsNullable())
{
typeName = prop.PropertyType.GetGenericArguments()[0].Name;
}
else
{
typeName = prop.PropertyType.Name;
}
switch (typeName)
{
case "String":
case "Boolean":
case "Byte":
case "TimeSpan":
case "Single":
case "Double":
case "Int16":
case "UInt16":
case "Int32":
case "UInt32":
case "Int64":
case "UInt64":
value = (prop.GetValue(source, null) ?? string.Empty).ToString();
break;
case "DateTime":
try
{
var tempDT = Convert.ToDateTime(prop.GetValue(source, null));
if (tempDT == DateTime.MinValue) continue;
value = tempDT.ToString("MM/dd/yyyy HH:mm:ss.fffffff");
}
catch(Exception ex)
{
continue;
}
break;
default:
var o = prop.GetValue(source, null);
XElement child;
if (o == null)
{
child = new XElement(prop.Name);
}
else
{
child = Serialize(o, prop.Name, includeNonPublicProperties);
}
element.Add(child);
valIsElement = true;
break;
}
}
if (!valIsElement)
{
element.AddAttribute(name, value);
}
}
return element;
}
关于c# - 带有 GetFields 和 GetProperties 的 DumpObject,例如带有嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13379431/
我正在查看 MSDN 作者在同一类的不同方法中使用以下代码的一些代码: if ( TypeDescriptor.GetProperties(ModelInstance)[propertyName] !
考虑以下代码。 Object obj; PropertyDescriptorCollection A = TypeDescriptor.GetProperties(obj); PropertyInfo
我有一种方法。在Java 8中,它在我的Macbook Pro M2上运行得很好。。结果是:。现在在迁移到Java 17之后,我得到了。我预计这两个版本的行为是相同的。我遗漏了什么?。我原以为这个方法
我有一种方法。在Java 8中,它在我的Macbook Pro M2上运行得很好。。结果是:。现在在迁移到Java 17之后,我得到了。我预计这两个版本的行为是相同的。我遗漏了什么?。我原以为这个方法
I have this method我有一种方法 static String getArchSuffix() { String arch = System.getProperty(&qu
System.getProperty("os.name") and System.getProperty("os.version") returning windows 10, 10.0, in wi
System.getProperty("os.name") and System.getProperty("os.version") returning windows 10, 10.0, in wi
我创建了包含用户列表的java类(称为“usersList”并包含每个值的用户名和密码), 现在我创建一个 JSP 文件并指定 JavaBeans 的范围: 我想将 usersList 获取到一
public object GetObjectToSerialize(object value, Type targetType) { var allProperties = value.
本文实例汇总了Java的System.getProperty()方法获取信息的用法。分享给大家供大家参考。具体如下: 复制代码代码如下: System.out.prin
我试图遍历类中的每个属性,输出属性的名称和值。但是我的代码没有返回任何属性。 正在循环的类: public class GameOptions { public ushort Fps;
我正在尝试编写一个通用实用程序,以便从 .NET 外部通过 COM 使用(/skip long story)。无论如何,我正在尝试向 ExpandoObject 添加属性,并且需要将 Property
我正在尝试使用 gradle 版本 4.8+ 的 Java、Serenity-BDD 项目,但应用程序没有提取 -Denvironment 和 -Dservicebranches 的 CLI 参数。我
我有一个源模块: import _ from 'underscore' import {Observable} from 'rxjs' export default function (rxfb) {
当我调用方法 System.getProperties(); 并打印它们时,它给出一个包含键值对的大列表,无需设置属性。 Java 从哪里获取那些属性来自? 最佳答案 来自托管 Java 的操作系统(
在这行代码中,我使用 getProperty 方法: PrintWriter writer = new PrintWriter("~/4413/ctrl/geo.txt".replaceFirst("
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
在一个非常庞大的代码库中,我发现了以下代码片段 System.getProperty("some stuff")。我尝试在一些 .properties 文件中查找该属性,但找不到它。你们有什么想法可以
让我大吃一惊! 根据 groovy 的文档,groovy 可以使用“getProperty”方法来获取对象的属性。所以当我想改变获取特殊对象属性的行为时,我使用一个类别类来覆盖“getProperty
这个问题可能会说明我缺乏关于 Groovy 类如何工作的知识,但我试图自己解决这个问题,但没有运气。我想在一个类上创建一个 getProperty() 方法,这样我就可以以 Groovyish 的方式
我是一名优秀的程序员,十分优秀!