我试图编写一个方法,通过将一个字符串数组作为参数传递给该方法,从另一个类执行静态方法.
这就是我所拥有的:
public static void
executeStaticCommand(final String[] command, Class<?> provider)
{
Method[] validMethods = provider.getMethods();
String javaCommand = TextFormat.toCamelCase(command[0]);
for (Method method : validMethods) {
if (method.getName().equals(javaCommand)) {
try {
method.invoke(null, new Object[] { new Object[] { command } });
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
Throwable ex = e.getCause();
ex.printStackTrace();
}
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样:
String[] args = new String[] { "methodName", "arg1", "arg2" };
executeStaticCommand(args, ClassName.class);
Run Code Online (Sandbox Code Playgroud)
应执行此操作:
public class ClassName {
public …Run Code Online (Sandbox Code Playgroud) 我想得到一个System.Type给定的string指定(原始)类型的C#友好名称,基本上是C#编译器在读取C#源代码时的方式.
我觉得描述我所追求的是以单元测试形式出现的最佳方式.
我希望存在一种通用技术可以使所有下面的断言通过,而不是试图对特殊C#名称的特殊情况进行硬编码.
Type GetFriendlyType(string typeName){ ...??... }
void Test(){
// using fluent assertions
GetFriendlyType( "bool" ).Should().Be( typeof(bool) );
GetFriendlyType( "int" ).Should().Be( typeof(int) );
// ok, technically not a primitive type... (rolls eyes)
GetFriendlyType( "string" ).Should().Be( typeof(string) );
// fine, I give up!
// I want all C# type-aliases to work, not just for primitives
GetFriendlyType( "void" ).Should().Be( typeof(void) );
GetFriendlyType( "decimal" ).Should().Be( typeof(decimal) );
//Bonus points: get type of fully-specified CLR types
GetFriendlyName( …Run Code Online (Sandbox Code Playgroud) 如果想要创建泛型的新实例,则需要定义新约束,如下所示:
public T SomeMethod<T>() where T : new()
{
return new T();
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用反射在没有新约束的情况下创建T的实例,如此(包含伪代码):
public T SomeMethod<T>()
{
if (T has a default constructor)
{
return a new instance of T;
}
else
{
return Factory<T>.CreateNew();
}
}
Run Code Online (Sandbox Code Playgroud) 我想要一个像这样的方法
def retrieve[T](value: Option[T])(implicit ct: ClassTag[T]): T;
Run Code Online (Sandbox Code Playgroud)
在这个方法中,我需要调用一个Java方法(超出我的控制范围)来创建一个T需要的实例Class[T]:
public <T> T construct(clazz: Class<T> /* other arguments */) { ... }
Run Code Online (Sandbox Code Playgroud)
我怎样才能Class[T]从ClassTag[T]?首先,我想我可以用runtimeClass从ClassTag,但它的类型Class[_],不是Class[T].或者是否有编译器可以自动提供的其他隐式值,我可以从中获取Class[T]?
首先,我要强调,这是稍微比一个不同的问题,这个线程.此外,安装KB2468871没有帮助.
我试图尽可能地简化这个问题.一般来说,它是关于使用Assembly.LoadFile(...)在Desktop应用程序中加载PCL程序集.
假设有一个.NET 4.0控制台应用程序(称为"C").它引用了.NET 4.0程序集(称为"N4")和PCL程序集(称为"PCL").
N4看起来像这样:
using System.Linq;
namespace N4
{
public class ClassInN4
{
public static string Greet()
{
return new string(
"hello from N4"
.ToCharArray()
.Select(char.ToUpper)
.ToArray()
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
PCL看起来像这样:
using System.Linq;
namespace PCL
{
public class ClassInPCL
{
public static string Greet()
{
return new string(
"hello from pcl"
.ToCharArray()
.Select(char.ToUpper)
.ToArray()
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和C看起来像这样:
using System;
using System.IO;
using System.Reflection;
using N4;
using PCL;
namespace C
{
internal …Run Code Online (Sandbox Code Playgroud) 认为我有一个类如下:
public class Foo
{
public int Bar { get; set; }
public int Sum(int a, int b)
{
return a + b;
}
public int Square(int a)
{
return a * a;
}
}
Run Code Online (Sandbox Code Playgroud)
你只知道我可以编写一个返回给定属性名称的方法:
var name = GetPropertyName<Foo>(f => f.Bar); //returns "Bar"
Run Code Online (Sandbox Code Playgroud)
GetPropertyName方法可以轻松实现,如下所示:
public static string GetPropertyName<T>(Expression<Func<T, object>> exp)
{
var body = exp.Body as MemberExpression;
if (body == null)
{
var ubody = (UnaryExpression)exp.Body;
body = ubody.Operand as MemberExpression;
}
return body.Member.Name;
}
Run Code Online (Sandbox Code Playgroud)
但我想像下面的属性名一样轻松获取方法名称:
var name1 = …Run Code Online (Sandbox Code Playgroud) 我有一个插件架构,其中在主应用程序中定义了一个抽象基类.它使用反射来加载具有基类的派生实现的程序集.我想获取包含派生对象的程序集的版本信息.这是我的基类:
namespace My.AppNameSpace
{
public abstract class BaseClass
{
public Version Version
{
get
{
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
return new Version(version);
}
}
public abstract void DoStuff();
}
}
Run Code Online (Sandbox Code Playgroud)
由于基类是主应用程序集的一部分,因此它始终返回正在执行的应用程序的版本信息,而不是包含派生类实现的.dll.我可以将版本访问器更改为abstract,并且它可以按照我的意愿运行.但是我需要在每个实现插件中添加相同的几行代码.
是否有一些技巧从一个单独的程序集中存在的基类中的代码中获取派生对象的版本信息?
与获取实现接口的所有类型相关,我们可以轻松地获得实现特定接口的Assembly中的所有类型.
例:
interface IFace
{
}
class Face : IFace
{
}
class TwoFace : Face
{
}
Run Code Online (Sandbox Code Playgroud)
对于这个结构,我们将通过反射找到这两个类,即使用第一个实现派生的所有类
GetTypes().Where(
type => type.GetInterfaces().Contains(typeof(IFace))
)
Run Code Online (Sandbox Code Playgroud)
所以问题是:如何将结果限制为最初实现接口的基类?! 在此示例中:只有类类型Face是相关的.
如何确定属性是否是用户定义的类型?我尝试使用IsClass,如下所示,但它的值对于String属性是真的(谁知道还有什么).
foreach (var property in type.GetProperties()) {
if (property.PropertyType.IsClass) {
// do something with property
}
}
Run Code Online (Sandbox Code Playgroud)
*更新以获得更清晰*
我试图遍历给定类型的定义,如果在程序集中定义了给定类型或其任何公共属性,我正在搜索嵌入式JavaScript文档.我只是不想在本机.NET类型上浪费处理资源和时间.
如果我有一个像LINQ这样的语句
x = Table.SingleOrDefault(o => o.id == 1).o.name;
Run Code Online (Sandbox Code Playgroud)
如何使用反射传入变量替换"id"和"name"?我尝试时不断将对象引用设置为对象错误的实例.我的尝试就是这样的
x = (string)Table.SingleOrDefault(o => (int?)o.GetType().GetProperty(idString)
.GetValue(o, null) == 1).GetType().GetField(nameString).GetValue(x);
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒.谢谢.