我上课了
public class Car
{
public string Name {get;set;}
public int Year {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
在单独的代码中,我有一个字段名称作为字符串(让我们使用"年")作为一个例子.
我想做这样的事情
if (Car.HasProperty("Year"))
Run Code Online (Sandbox Code Playgroud)
这将弄清楚汽车对象上是否有一个Year字段.这将返回真实.
if (Car.HasProperty("Model"))
Run Code Online (Sandbox Code Playgroud)
将返回false.
我看到代码循环遍历属性,但想看看是否有更简洁的方法来确定是否存在单个字段.
在C#中,如果使用Type.GetFields()表示派生类的类型,它将返回a)派生类中所有显式声明的字段,b)派生类中自动属性的所有支持字段和c)基础中所有显式声明的字段类.
为什么缺少基类中自动属性的d)支持字段?
例:
public class Base {
public int Foo { get; set; }
}
public class Derived : Base {
public int Bar { get; set; }
}
class Program {
static void Main(string[] args) {
FieldInfo[] fieldInfos = typeof(Derived).GetFields(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.FlattenHierarchy
);
foreach(FieldInfo fieldInfo in fieldInfos) {
Console.WriteLine(fieldInfo.Name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将只显示Bar的支持字段,而不是Foo.
刚开始学习Ruby元编程.看看Object.methods我得到:
Object.methods => [
:allocate,
:new,
:superclass,
:freeze,
:===,
:==,
:<=>,
:<,
:<=,
:>,
:>=,
:to_s,
:included_modules,
:include?,
:name,
:ancestors,
:instance_methods,
:public_instance_methods,
:protected_instance_methods,
:private_instance_methods,
:constants,
:const_get,
:const_set,
:const_defined?,
:const_missing,
:class_variables,
:remove_class_variable,
:class_variable_get,
:class_variable_set,
:class_variable_defined?,
:module_exec,
:class_exec,
:module_eval,
:class_eval,
:method_defined?,
:public_method_defined?,
:private_method_defined?,
:protected_method_defined?,
:public_class_method,
:private_class_method,
:autoload,
:autoload?,
:instance_method,
:public_instance_method,
:nil?,
:=~,
:!~,
:eql?,
:hash,
:class,
:singleton_class,
:clone,
:dup,
:initialize_dup,
:initialize_clone,
:taint,
:tainted?,
:untaint,
:untrust,
:untrusted?,
:trust,
:frozen?,
:inspect,
:methods,
:singleton_methods,
:protected_methods,
:private_methods,
:public_methods,
:instance_variables,
:instance_variable_get,
:instance_variable_set,
:instance_variable_defined?,
:instance_of?,
:kind_of?,
:is_a?,
:tap, …Run Code Online (Sandbox Code Playgroud) 给出以下示例代码;
class Program
{
static void Main(string[] args)
{
var results = GetChildren().ToList();
}
static IEnumerable<MyBaseClass> GetChildren()
{
return Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.BaseType == typeof(MyBaseClass))
.Select(o => (MyBaseClass)Activator.CreateInstance(o, null));
}
}
abstract class MyBaseClass
{ }
class MyChildClass : MyBaseClass
{
public MyChildClass(object paramOne)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误;
MissingMethodException:找不到类型"CreateInstanceCtorIssue.MyChildClass"的构造函数.
但是,如果我添加一个无参数构造函数,它会创建对象OK.
我试图弄清楚为什么我要为CreateInstance提供的参数不会导致它找到正确的构造函数.有人有任何想法吗?
我创建了一个静态类并在Reflection中使用它.但是当我访问该类的方法时,它显示了5个方法但我只创建了1个.额外的方法是
Write
ToString
Equals
GetHashCode
GetType
Run Code Online (Sandbox Code Playgroud)
但我只创建了Write方法.
一个静态方法可以在静态类中,但这些额外的4种方法不是静态方法,而是从它们驱动的位置开始.什么是基类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ReflectionDemo
{
static class ReflectionTest
{
public static int Height;
public static int Width;
public static int Weight;
public static string Name;
public static void Write()
{
Type type = typeof(ReflectionTest); //Get type pointer
FieldInfo[] fields = type.GetFields(); //obtain all fields
MethodInfo[] methods = type.GetMethods();
Console.WriteLine(type);
foreach (var item in methods)
{
string name = item.Name;
Console.WriteLine(name);
}
foreach (var field …Run Code Online (Sandbox Code Playgroud) 以下代码工作正常.
LibraryTests::TestGetServer();
Run Code Online (Sandbox Code Playgroud)
获取LibraryTests中的函数数组并运行它们:
$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
call_user_func('LibraryTests::' . $method . '()' );
}
Run Code Online (Sandbox Code Playgroud)
这会引发错误: Warning: call_user_func(LibraryTests::TestGetServer()) [function.call-user-func]: First argument is expected to be a valid callback
这是被调用的类:
class LibraryTests extends TestUnit {
function TestGetServer() {
TestUnit::AssertEqual(GetServer(), "localhost/");
}
.
.
.
Run Code Online (Sandbox Code Playgroud)
怎么修?
在PHP 5.2.8中工作.
我在StackOverflow上的第一个问题.我希望能够做到这样的事情:
SomeClass mock = mock(SomeClass.class);
String methodName ="someMethod"; 或方法方法= ... someMethod ...
这两件事(模拟和方法)将结合起来执行以下操作:
当(mock.someMethod())thenReturn(空).
当然,'null'值会根据我的需要进行相应更改,但我想确定两件事:
1),它甚至有可能做一些像这样在Java中? 这 =将类对象和方法组合到methodCall中.
2)我怎么做这样的事情?
我无休止地研究了这个,我找不到任何东西.问题是即使这适用于常规类和常规方法(someClass和someMethod会聚在一起做someClass.someMethod()),请记住,这必须与模拟对象一起使用才能在when中使用( )打电话.
答案:when(method.invoke(mock)).thenReturn("Hello world."); 是正确的语法和反射确实在when()调用内工作.谢谢Kevin Welker!
我需要动态创建对象.我使用Spring创建了一个类名映射.现在我可以用了
弹簧 ApplicationContext.getbean(className)
要么
Java Class.forName(className).newInstance().
哪种方法更有效?
谢谢.
我正在尝试使用Scala的Java库(JOhm)并注意到当lib试图用类似的东西读取我的Scala类的字段时它会失败model.getClass().getDeclaredFields().
然后我决定尝试使用Scala解释器中的简单示例:
scala> import java.lang.reflect.Field;
import java.lang.reflect.Field
scala> class myClass(attribute1: String, attribute2: String, attribute3: String)
defined class myClass
scala> val myInstance = new myClass("value1", "value2", "value3")
myInstance: myClass = myClass@7055c39a
scala> myInstance.getClass().getDeclaredFields()
res0: Array[java.lang.reflect.Field] = Array()
Run Code Online (Sandbox Code Playgroud)
实际上,我们根本没有任何领域.
现在,如果我尝试这个怎么办:
scala> class myClass2(attribute1: String, attribute2: String, attribute3: String) { override def toString = this.attribute1 }
defined class myClass2
scala> val myInstance2 = new myClass2("value1", "value2", "value3")
myInstance2: myClass2 = value1
scala> myInstance2.getClass().getDeclaredFields()
res1: Array[java.lang.reflect.Field] = Array(private final java.lang.String myClass2.attribute1)
Run Code Online (Sandbox Code Playgroud)
因此,如果使用其中一个类'方法中的一个字段,则可以通过getDeclaredFields()找到它.我在这里错过了什么?
我想从我自己的代码中调用.net可执行文件中的函数.我用反射器看到了这个:
namespace TheExe.Core
{
internal static class AssemblyInfo
internal static class StringExtensionMethods
}
Run Code Online (Sandbox Code Playgroud)
在命名空间中,TheExe.Core是我感兴趣的函数:
internal static class StringExtensionMethods
{
// Methods
public static string Hash(this string original, string password);
// More methods...
}
Run Code Online (Sandbox Code Playgroud)
使用此代码我可以看到哈希方法,但我该如何调用它?
Assembly ass = Assembly.LoadFile("TheExe");
Type asmType = ass.GetType("TheExe.Core.StringExtensionMethods");
MethodInfo mi = asmType.GetMethod("Hash", BindingFlags.Public | BindingFlags.Static);
string[] parameters = { "blabla", "MyPassword" };
// This line gives System.Reflection.TargetParameterCountException
// and how to cast the result to string ?
mi.Invoke(null, new Object[] {parameters});
Run Code Online (Sandbox Code Playgroud)