我有一个模板类如下:
class MyClass<T>
{
T field;
public void myMethod()
{
field = new T(); // gives compiler error
}
}
Run Code Online (Sandbox Code Playgroud)
如何在班级中创建T的新实例?
我今天试图做一些常规测试,并且Assembly.GetTypes()当我偶然发现某事时,将所有类型都放入一个程序集中(通过调用):
System.RuntimeType:[First.Namespace.FirstClass]
Run Code Online (Sandbox Code Playgroud)
每当我尝试比较那种类型时typeof(FirstClass),它们就不相同.所以,当我试图找到包含FirstClass作为通用参数的所有类型时,我找不到任何类型.
System.RuntimeType和之间有什么区别System.Type?
有什么方法可以解决我的问题吗?
我有一个Class对象.我想确定Class对象表示的类型是否实现了特定的接口.我想知道如何实现这一目标?
我有以下代码.基本上它的作用是获取指定包中所有类的数组.然后我想通过数组并将实现接口的Class对象添加到我的地图中.问题是isInstance()将对象作为参数.我无法实例化一个接口.所以我对此感到很茫然.有任何想法吗?
Class[] classes = ClassUtils.getClasses(handlersPackage);
for(Class clazz : classes)
{
if(clazz.isInstance(/*Some object*/)) //Need something in this if statement
{
retVal.put(clazz.getSimpleName(), clazz);
}
}
Run Code Online (Sandbox Code Playgroud) WCF反序列化有一些神奇之处.如何在不调用其构造函数的情况下实例化数据协定类型的实例?
例如,考虑这个数据合同:
[DataContract]
public sealed class CreateMe
{
[DataMember] private readonly string _name;
[DataMember] private readonly int _age;
private readonly bool _wasConstructorCalled;
public CreateMe()
{
_wasConstructorCalled = true;
}
// ... other members here
}
Run Code Online (Sandbox Code Playgroud)
通过DataContractSerializer您获取此对象的实例时,您将看到该字段_wasConstructorCalled是false.
那么,WCF如何做到这一点?这是其他人可以使用的技术,还是隐藏在我们之外?
是否有内置的方法,函数,API,普遍接受的方式等来转储Objective C中实例化对象的内容,特别是在Apple的Cocoa/Cocoa-Touch环境中?
我希望能够做类似的事情
MyType *the_thing = [[MyType alloc] init];
NSString *the_dump = [the_thing dump]; //pseudo code
NSLog("Dumped Contents: %@", the_dump);
Run Code Online (Sandbox Code Playgroud)
并显示对象的实例变量名称和值,以及可在运行时调用的任何方法.理想情况下,易于阅读的格式.
对于熟悉PHP的开发人员,我基本上寻找等效的反射函数(var_dump(),get_class_methods())和OO Reflection API.
我想获取特定属性的PropertyInfo.我可以用:
foreach(PropertyInfo p in typeof(MyObject).GetProperties())
{
if ( p.Name == "MyProperty") { return p }
}
Run Code Online (Sandbox Code Playgroud)
但必须有办法做类似的事情
typeof(MyProperty) as PropertyInfo
Run Code Online (Sandbox Code Playgroud)
在那儿?还是我坚持做一个类型不安全的字符串比较?
干杯.
请考虑以下示例代码:
class SampleClass
{
public long SomeProperty { get; set; }
}
public void SetValue(SampleClass instance, decimal value)
{
// value is of type decimal, but is in reality a natural number => cast
instance.SomeProperty = (long)value;
}
Run Code Online (Sandbox Code Playgroud)
现在我需要通过反思来做类似的事情:
void SetValue(PropertyInfo info, object instance, object value)
{
// throws System.ArgumentException: Decimal can not be converted to Int64
info.SetValue(instance, value)
}
Run Code Online (Sandbox Code Playgroud)
请注意,我不能假设PropertyInfo总是表示long,而且该值始终不是小数.但是,我知道可以将值转换为该属性的正确类型.
如何通过反射将'value'参数转换为PropertyInfo实例表示的类型?
我通过反射检查对象的属性,并继续处理每个属性的数据类型.这是我的(减少的)来源:
private void ExamineObject(object o)
{
Type type = default(Type);
Type propertyType = default(Type);
PropertyInfo[] propertyInfo = null;
type = o.GetType();
propertyInfo = type.GetProperties(BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance);
// Loop over all properties
for (int propertyInfoIndex = 0; propertyInfoIndex <= propertyInfo.Length - 1; propertyInfoIndex++)
{
propertyType = propertyInfo[propertyInfoIndex].PropertyType;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我新近需要处理可空属性,但我不知道如何获得可空属性的类型.
我试图找出如何在C#应用程序中运行时导入和使用.dll.使用Assembly.LoadFile()我设法让我的程序加载dll(这部分肯定是有效的,因为我能够获得带有ToString()的类的名称),但是我无法使用'输出'我的控制台应用程序内的方法.我正在编译.dll然后将其移动到我的控制台项目中.CreateInstance之间是否有额外的步骤,然后能够使用这些方法?
这是我的DLL中的类:
namespace DLL
{
using System;
public class Class1
{
public void Output(string s)
{
Console.WriteLine(s);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我想要加载DLL的应用程序
namespace ConsoleApplication1
{
using System;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");
foreach(Type type in DLL.GetExportedTypes())
{
var c = Activator.CreateInstance(type);
c.Output(@"Hello");
}
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)