鉴于你有一个接受的功能的场景t interface{}.如果确定t是切片,我range该如何切片?我不会知道传入的类型,例如[]string,[]int或者[]MyType,在编译时.
func main() {
data := []string{"one","two","three"}
test(data)
moredata := []int{1,2,3}
test(data)
}
func test(t interface{}) {
switch reflect.TypeOf(t).Kind() {
case reflect.Slice:
// how do I iterate here?
for _,value := range t {
fmt.Println(value)
}
}
}
Run Code Online (Sandbox Code Playgroud)
public bool IsList(object value)
{
Type type = value.GetType();
// Check if type is a generic list of any type
}
Run Code Online (Sandbox Code Playgroud)
检查给定对象是列表还是可以转换为列表的最佳方法是什么?
我有班级和财产.一些属性可以标记属性(它是我的LocalizedDisplayName继承DisplayNameAttribute).这是获取类的所有属性的方法:
private void FillAttribute()
{
Type type = typeof (NormDoc);
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想在列表框中添加类的属性,在列表框中标记LocalizedDisplayName并显示属性的值.我怎样才能做到这一点?
编辑
这是LocalizedDisplayNameAttribute:
public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
public LocalizedDisplayNameAttribute(string resourceId)
: base(GetMessageFromResource(resourceId))
{ }
private static string GetMessageFromResource(string resourceId)
{
var test =Thread.CurrentThread.CurrentCulture;
ResourceManager manager = new ResourceManager("EArchive.Data.Resources.DataResource", Assembly.GetExecutingAssembly());
return manager.GetString(resourceId);
}
}
Run Code Online (Sandbox Code Playgroud)
我想从资源文件中获取字符串.谢谢.
我试着知道一个属性是否存在于类中,我试过这个:
public static bool HasProperty(this object obj, string propertyName)
{
return obj.GetType().GetProperty(propertyName) != null;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么第一种测试方法没有通过?
[TestMethod]
public void Test_HasProperty_True()
{
var res = typeof(MyClass).HasProperty("Label");
Assert.IsTrue(res);
}
[TestMethod]
public void Test_HasProperty_False()
{
var res = typeof(MyClass).HasProperty("Lab");
Assert.IsFalse(res);
}
Run Code Online (Sandbox Code Playgroud) 在C#中是否有一种使用反射来确定方法是否已作为扩展方法添加到类的技术?
给定一个扩展方法,如下所示,可以确定Reverse()已被添加到字符串类?
public static class StringExtensions
{
public static string Reverse(this string value)
{
char[] cArray = value.ToCharArray();
Array.Reverse(cArray);
return new string(cArray);
}
}
Run Code Online (Sandbox Code Playgroud)
我们正在寻找一种机制,在单元测试中确定扩展方法是由开发人员适当添加的.尝试这种情况的一个原因是,开发人员可能会在实际的类中添加类似的方法,如果是,编译器将选择该方法.
我有一个基本上是另一个类的副本的类.
public class A {
int a;
String b;
}
public class CopyA {
int a;
String b;
}
Run Code Online (Sandbox Code Playgroud)
我正在做的是在通过webservice调用发送之前将类中的值A放入.现在我想创建一个反射方法,它基本上将所有相同(按名称和类型)的字段从一个类复制到另一个类.CopyACopyAACopyA
我怎样才能做到这一点?
这是我到目前为止所做的,但它并不常用.我认为这里的问题是我试图在我循环的字段上设置一个字段.
private <T extends Object, Y extends Object> void copyFields(T from, Y too) {
Class<? extends Object> fromClass = from.getClass();
Field[] fromFields = fromClass.getDeclaredFields();
Class<? extends Object> tooClass = too.getClass();
Field[] tooFields = tooClass.getDeclaredFields();
if (fromFields != null && tooFields != null) {
for (Field tooF : tooFields) {
logger.debug("toofield name #0 and …Run Code Online (Sandbox Code Playgroud) 我有一个方法,我需要解决类的类型.此类存在于另一个程序集中,该命名空间类似于:
MyProject.Domain.Model
Run Code Online (Sandbox Code Playgroud)
我试图执行以下操作:
Type.GetType("MyProject.Domain.Model." + myClassName);
Run Code Online (Sandbox Code Playgroud)
如果执行此操作的代码与我尝试解析其类型的类在同一程序集中,则此方法很有用,但是,如果我的类位于不同的程序集中,则此代码将失败.
我确信有一个更好的方法来完成这个任务,但是我没有很多经验来解析程序集和遍历名称空间来解决我正在寻找的类的类型.有关更优雅地完成此任务的任何建议或提示吗?
我需要按照在类中声明它们的顺序使用反射来获取所有属性.根据MSDN,使用时无法保证订单GetProperties()
GetProperties方法不以特定顺序返回属性,例如按字母顺序或声明顺序.
但我已经读过,通过订购属性有一个解决方法MetadataToken.所以我的问题是,这样安全吗?我似乎无法在MSDN上找到有关它的任何信息.或者有没有其他方法来解决这个问题?
我目前的实施情况如下:
var props = typeof(T)
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.OrderBy(x => x.MetadataToken);
Run Code Online (Sandbox Code Playgroud) 我希望在Python中创建一个可以添加和删除属性和方法的类.我该怎么做呢?
哦,请不要问为什么.
reflection ×10
c# ×7
.net ×3
generics ×1
go ×1
go-reflect ×1
java ×1
list ×1
namespaces ×1
properties ×1
python ×1
runtime ×1
slice ×1