我对Interface和BeanInfo Introspector中的默认方法有一点问题.在这个例子中,有接口:Interface
public static interface Interface {
default public String getLetter() {
return "A";
}
}
Run Code Online (Sandbox Code Playgroud)
以及ClassA和ClassB两个类:
public static class ClassA implements Interface {
}
public static class ClassB implements Interface {
public String getLetter() {
return "B";
}
}
Run Code Online (Sandbox Code Playgroud)
在main方法应用程序中打印来自BeanInfo的PropertyDescriptors:
public static String formatData(PropertyDescriptor[] pds) {
return Arrays.asList(pds).stream()
.map((pd) -> pd.getName()).collect(Collectors.joining(", "));
}
public static void main(String[] args) {
try {
System.out.println(
formatData(Introspector.getBeanInfo(ClassA.class)
.getPropertyDescriptors()));
System.out.println(
formatData(Introspector.getBeanInfo(ClassB.class)
.getPropertyDescriptors()));
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
class
class, letter …
Run Code Online (Sandbox Code Playgroud) 我怎样才能获得PropertyDescriptor
当前的房产?例如:
[MyAttribute("SomeText")]
public string MyProperty
{
get{....}
set
{
// here I want to get PropertyDescriptor for this property.
}
}
Run Code Online (Sandbox Code Playgroud) C#,VS 2010.有人,请解释为什么我不能var
在下面的代码中使用!
var props = TypeDescriptor.GetProperties(adapter);
// error CS1061: 'object' does not contain a definition for 'DisplayName'
foreach (var prop in props)
{
string name = prop.DisplayName;
}
// No error
foreach (PropertyDescriptor prop in props)
{
string name = prop.DisplayName;
}
Run Code Online (Sandbox Code Playgroud)
TypeDescriptor.GetProperties
返回PropertyDescriptorCollection
带有实例的PropertyDescriptor
.为什么编译器看不到这个?
像这样设置属性描述符:
Object.defineProperty(window, 'someFunction', {
value: function() { alert('safe'); },
writable: false,
enumerable: false,
configurable: false
});
Run Code Online (Sandbox Code Playgroud)
......据我所知,应该使不可写的someFunction
财产window
.它适用于我期望的函数表达式,无论函数是否直接赋值给对象属性... 小提琴
window.someFunction = function() { alert('boom!'); }
someFunction(); // safe
Run Code Online (Sandbox Code Playgroud)
...或分配给全局变量:小提琴
var someFunction = function() { alert('boom!'); }
someFunction(); // safe
Run Code Online (Sandbox Code Playgroud)
但是,它似乎不适用于函数声明:小提琴
function someFunction() { alert('boom!'); }
someFunction(); // boom!
Run Code Online (Sandbox Code Playgroud)
这种行为是故意的吗?它背后的原因是什么?这记录在哪里?或者我只是犯了一些愚蠢的错误?
顺便说一句,我正在使用Chromium 17来测试它.严格模式似乎没有任何区别.
javascript function global-variables propertydescriptor ecmascript-5
我希望自动显示每个IList
在我的可扩展PropertyGrid
(通过"可扩展",我显然意味着将显示项目).我不想在每个列表上使用属性(再次,我希望它适用于每个列表IList
)
我试图通过使用自定义PropertyDescriptor
和a来实现它ExpandableObjectConverter
.它工作正常,但在我从列表中删除项目后,PropertyGrid
没有刷新,仍然显示已删除的项目.
我尝试使用ObservableCollection
同时提高OnComponentChanged
,也RefreshProperties
属性,但没有任何效果.
这是我的代码:
public class ExpandableCollectionPropertyDescriptor : PropertyDescriptor
{
private IList _collection;
private readonly int _index = -1;
internal event EventHandler RefreshRequired;
public ExpandableCollectionPropertyDescriptor(IList coll, int idx) : base(GetDisplayName(coll, idx), null)
{
_collection = coll
_index = idx;
}
public override bool SupportsChangeEvents
{
get { return true; }
}
private static string GetDisplayName(IList list, int index)
{
return "[" + …
Run Code Online (Sandbox Code Playgroud) 我正在PropertyGrid
通过实现自定义对象类型的显示方式ICustomTypeDescriptor
.我允许用户创建自己的自定义属性,这些属性存储在单个键和值字典中.我能够PropertyDescriptors
为这些值创建所有值并在属性网格中查看它们.但是,我还想显示所有默认属性,如果PropertyGrid
通过反射填充而不是覆盖ICustomTypeDescriptor.GetProperties
方法,则会显示这些属性.
现在我知道如何获取对象的类型,然后GetProperties()
,但这会返回一个PropertyInfo
not 数组ProperyDescriptor
.那么如何PropertyInfo
将该类型的对象转换为PropertyDescriptor
对象以包含到我的集合中PropertyDescriptors
?
//gets the local intrinsic properties of the object
Type thisType = this.GetType();
PropertyInfo[] thisProps = thisType.GetProperties();
//this line obviously doesn't work because the propertydescriptor
//collection needs an array of PropertyDescriptors not PropertyInfo
PropertyDescriptorCollection propCOl =
new PropertyDescriptorCollection(thisProps);
Run Code Online (Sandbox Code Playgroud) 我试图在Android应用程序中使用第三方jar文件.我已经能够使用jar文件中的一些类了.但是,其中一个类引用了dalvik vm似乎不支持的某些Java类.这些是我在LogCat中看到的一些错误:
Unable to find class referenced in signature java/beans/PropertyDescriptor.
Unable to resolve virtual method java/beans/PropertyDescriptor.getName().
Unable to resolve virtual method java/beans/PropertyDescriptor.getReadMethod().
Unable to resolve static method java/beans/Introspector.getBeanInfo().
Unable to resolve exception class java/beans/IntrospectionException.
Run Code Online (Sandbox Code Playgroud)
似乎dalvik不支持与内省和反射相关的Java类.我想找出两件事.这是否计划在不久的将来在达尔维克支持这一点?其次,是否有人建议可以解决这个问题?
通常Python描述符被定义为类属性.但在我的情况下,我希望每个对象实例都有不同的设置描述符,这取决于输入.例如:
class MyClass(object):
def __init__(self, **kwargs):
for attr, val in kwargs.items():
self.__dict__[attr] = MyDescriptor(val)
Run Code Online (Sandbox Code Playgroud)
每个对象都具有在实例化时确定的不同属性集.由于这些是一次性对象,因此首先将它们子类化是不方便的.
tv = MyClass(type="tv", size="30")
smartphone = MyClass(type="phone", os="android")
tv.size # do something smart with the descriptor
Run Code Online (Sandbox Code Playgroud)
将描述符分配给对象似乎不起作用.如果我尝试访问该属性,我会得到类似的东西
<property at 0x4067cf0>
Run Code Online (Sandbox Code Playgroud)
你知道为什么这不起作用吗?有什么工作吗?
我想在我的中显示一个类的多个实例PropertyGrid
.这个类看起来像这样:
public class Parameter
{
[Description("the name")]
public string Name { get; set; }
[Description("the value"), ReadOnly(true)]
public string Value { get; set; }
[Description("the description")]
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有很多类的实例TreeView
.当我在我选择其中一个时TreeView
,属性会PropertyGrid
按预期显示.到目前为止一切顺利,但我想以下列方式自定义此行为:
对于每个单个实例,我希望能够阻止用户修改特定属性.通过ReadOnly(true)
在我的类中设置(如上面的示例中所示),Value
将在类级别禁用所有属性.
经过一些研究后,我发现以下解决方案让我有机会在运行时启用/禁用特定属性:
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this)["Value"];
ReadOnlyAttribute attr =
(ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo isReadOnly = attr.GetType().GetField(
"isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attr, false);
Run Code Online (Sandbox Code Playgroud)
这种方法运行得很好但不幸的是也只在类级别上.这意味着,如果我设定Value
的isReadOnly
到false
,所有我的Parameter
-objects拥有 …
我知道,你可以得到一定的财产"的属性描述对象prop
的某个对象的" obj
有
Object.getOwnPropertyDescriptor(obj,"prop");
.我只是想知道:这些物品存放在哪里?它们是在内部存储在一个对象中还是......在其他地方 我试图在开发人员工具中找到它们,但没有运气.
c# ×5
.net ×3
propertygrid ×3
javascript ×2
android ×1
dalvik ×1
ecmascript-5 ×1
function ×1
interface ×1
java ×1
java-8 ×1
javabeans ×1
propertyinfo ×1
python ×1
var ×1