我想知道是否有任何额外的优化我可以实现以提高Java中反射调用的速度.并不是说性能是令人望而却步的,但是我在考虑库中的某些代码时得到了一些我正在编写的代码在某个地方以紧密的方式实现.
考虑一种反射性调用的实用方法:
public static Object invoke(Object targetObject, String methodName, Object[] arguments, Class<?>[] signature)
Run Code Online (Sandbox Code Playgroud)
基本操作是
return method.invoke(targetObject, arguments);
Run Code Online (Sandbox Code Playgroud)
作为性能优化,我使用目标对象的类,方法名称和签名(其代码可能使用某些改进)的哈希来缓存方法,但除此之外,还有什么我可以做的吗?我听说过InvokeDynamic的一些早期实现的参考听起来很有希望,但我只是假设它们可能还不适用,并且我打算使用自己的字节代码操作,因为我希望保持实用程序简单(但速度快).
干杯.
给出2个接口:
public interface BaseInterface<T> { }
public interface ExtendedInterface<T0, T1> extends BaseInterface<T0> {}
Run Code Online (Sandbox Code Playgroud)
和一个具体的类:
public class MyClass implements ExtendedInterface<String, Object> { }
Run Code Online (Sandbox Code Playgroud)
如何找到传递给BaseInterface接口的类型参数?
(我可以通过调用类似的东西来检索ExtendedInterface类型参数
MyClass.class.getGenericInterfaces()[0].getActualTypeArguments()
Run Code Online (Sandbox Code Playgroud)
但我无法找到一种简单的方法来递归到任何基础通用接口并得到任何有意义的东西).
我会问一个听起来很奇怪的问题.
有没有办法在运行时构建一个新类?或者至少,向现有类添加新属性.
我的意思是创建一个不存在的类,而不是现有类的实例.我以后可以使用反射来加载和使用这个类.
如果我写:
var type = typeof(List<string>);
Console.WriteLine(type.Name);
Run Code Online (Sandbox Code Playgroud)
它会写:
List`1
我想要它只写:
名单
我怎样才能做到这一点?有没有更聪明的方法来做到这一点,而不必使用Substring或类似的字符串操作功能?
我在一个名为"A7" 的refrenced程序集(名为"DAL")中有一个静态类:
A7是这样的:
public static class A7
{
public static readonly bool NeedCoding = false;
public static readonly string Title = "Desc_Title"
public static readonly string F0 = "";
public static readonly string F1 = "Desc_F1";
public static readonly string F2 = "Desc_F2";
public static readonly string F3 = "Desc_F3";
public static readonly string F4 = "Desc_F4";
}
Run Code Online (Sandbox Code Playgroud)
如何从DAL汇编A7类获取所有属性名称和值?
谢谢
我试图使用反射动态地将值设置为类的嵌套属性.任何人都可以帮我这样做.
我正在上课Region.
public class Region
{
public int id;
public string name;
public Country CountryInfo;
}
public class Country
{
public int id;
public string name;
}
Run Code Online (Sandbox Code Playgroud)
我有一个Oracle数据阅读器来提供Ref游标中的值.
这会给我一个
ID,姓名,COUNTRY_ID,COUNTRY_NAME
我可以通过下面的方式将值分配给Region.Id,Region.Name.
FieldName="id"
prop = objItem.GetType().GetProperty(FieldName, BindingFlags.Public | BindingFlags.Instance);
prop.SetValue(objItem, Utility.ToLong(reader_new[ResultName]), null);
Run Code Online (Sandbox Code Playgroud)
对于嵌套属性,我可以通过读取Fieldname创建实例来为下面的值赋值.
FieldName="CountryInfo.id"
if (FieldName.Contains('.'))
{
Object NestedObject = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split('.')[0]), BindingFlags.Public | BindingFlags.Instance);
//getting the Type of NestedObject
Type NestedObjectType = NestedObject.GetType();
//Creating Instance
Object Nested = Activator.CreateInstance(typeNew);
//Getting the nested Property
PropertyInfo nestedpropinfo = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split('.')[0]), BindingFlags.Public | …Run Code Online (Sandbox Code Playgroud) 有没有办法在VBS中列出已创建对象的可用方法?
例如:
Set IE = CreateObject("InternetExplorer.Application")
Run Code Online (Sandbox Code Playgroud)
我想列出此对象的可用属性,如下所示:
IE.AddressBar
IE.Application
IE.Busy
...
Run Code Online (Sandbox Code Playgroud)
或方法:
IE.ClientToWindow
IE.ExecWB
IE.GetProperty
...
Run Code Online (Sandbox Code Playgroud)
如何在VBS中发现任意有效对象的可用属性?
只是奇怪的是,当我反思所有类型时,我偶然发现了好奇心来检查其他东西.
为什么System.__ComObject集会的阶级mscorlib.dll(有时?)声称是公开的,而事实上它似乎是非公开的?如果我在一个简单的C#控制台应用程序中运行以下代码:
var t = Type.GetType("System.__ComObject");
Console.WriteLine(t.IsPublic); // "True" ?!
Console.WriteLine(t.IsVisible); // "False"
Run Code Online (Sandbox Code Playgroud)
输出似乎有冲突.非嵌套类型(t.IsNested为false)应为IsPublic和提供相同的真值IsVisible.当我看到装配时,IL DASM我看到:
.class private auto ansi beforefieldinit System.__ComObject
extends System.MarshalByRefObject
{
} // end of class System.__ComObject
Run Code Online (Sandbox Code Playgroud)
对我来说,它看起来非常像非公开类,这些对应于下面的C#代码:
namespace System
{
// not public
internal class __ComObject : MarshalByRefObject
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
当我比较其他类型的具有类似的名称,System.__Canon以及类似IL改性剂,既IsPublic与IsVisible预期返回false.
有谁知道为什么(和什么时候)Type.GetType("System.__ComObject").IsPublic给出了真实的?
我发现这个问题有这个很好的答案:
我玩弄了答案并尝试以相同的方式获取结构的名称:
package main
import (
"fmt"
"reflect"
)
type Ab struct {
}
func getType(myvar interface{}) string {
return reflect.TypeOf(myvar).Name()
}
func main() {
fmt.Println("Hello, playground")
tst := "string"
tst2 := 10
tst3 := 1.2
tst4 := new(Ab)
fmt.Println(getType(tst))
fmt.Println(getType(tst2))
fmt.Println(getType(tst3))
fmt.Println(getType(tst4))
}
Run Code Online (Sandbox Code Playgroud)
去游乐场:http://play.golang.org/p/tD8mygvETH
但输出是:
Hello, playground
string
int
float64
Program exited.
Run Code Online (Sandbox Code Playgroud)
预期产出将是:
Hello, playground
string
int
float64
Ab
Program exited.
Run Code Online (Sandbox Code Playgroud)
我试着通过阅读文档来弄清楚,但没有找到相关的问题.所以,对于非常普遍的问题感到抱歉,但是:
是什么原因,reflect.TypeOf().Name()不适用于(this)struct(s)?
为了避免在搜索已知类型的属性时使用过时的非泛型语法,通常会使用System.Reflection.CustomAttributeExtensions类中的扩展方法(从.NET 4.5开始).
但是,如果在重写方法的返回参数(或重写的属性/索引器的访问者)上搜索属性,则这似乎会失败.
我正在使用.NET 4.6.1体验这一点.
简单复制(完整):
using System;
using System.Reflection;
namespace ReflectionTrouble
{
class B
{
//[return: MyMark("In base class")] // uncommenting does not help
public virtual int M() => 0;
}
class C : B
{
[return: MyMark("In inheriting class")] // commenting away attribute does not help
public override int M() => -1;
}
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)] // commenting away AttributeUsage does not help
sealed class MyMarkAttribute : Attribute
{ …Run Code Online (Sandbox Code Playgroud) reflection ×10
c# ×6
.net ×4
generics ×2
java ×2
c#-4.0 ×1
cil ×1
go ×1
performance ×1
propertyinfo ×1
struct ×1
types ×1
vb.net ×1
vbscript ×1