所以我有一个泛型的类,它可能需要在它的方法中自己创建一个自己的实例,使用不同类型的泛型,这种类型是通过relfection获得的.
这很重要,因为这个Repository将T映射到数据库表[这是我正在编写的ORMish],如果表示T的类有一个表示ANOTHER表的集合,我需要能够实例化并将其传递给存储库[ala Inception ].
我提供的方法是为了让它更容易看到问题.
private PropertiesAttributesAndRelatedClasses GetPropertyAndAttributesCollection()
{
// Returns a List of PropertyAndAttributes
var type = typeof(T);
//For type T return an array of PropertyInfo
PropertiesAttributesAndRelatedClasses PAA = new PropertiesAttributesAndRelatedClasses();
//Get our container ready
PropertyAndAttributes _paa;
foreach (PropertyInfo Property in type.GetProperties())
//Let's loop through all the properties.
{
_paa = new PropertyAndAttributes();
//Create a new instance each time.
_paa.AddProperty(Property);
//Adds the property and generates an internal collection of attributes for it too
bool MapPropertyAndAttribute = true;
if (Property.PropertyType.Namespace …Run Code Online (Sandbox Code Playgroud) 当我阅读Effective Java时,作者告诉我单元素enum类型是实现单例的最佳方式,因为我们不必考虑复杂的序列化或反射攻击.这意味着我们无法创建enum使用反射的实例,对吧?
我做了一些测试,enum这里有一个类:
public enum Weekday {}
Run Code Online (Sandbox Code Playgroud)
然后我尝试创建一个实例Weekday:
Class<Weekday> weekdayClass = Weekday.class;
Constructor<Weekday> cw = weekdayClass.getConstructor(null);
cw.setAccessible(true);
cw.newInstance(null);
Run Code Online (Sandbox Code Playgroud)
如你所知,它不起作用.当我改变关键词enum时class,它有效.我想知道为什么.谢谢.
在.NET 4中创建在运行时确定的类型实例的最佳方法是什么?
我有一个实例方法,虽然作用于BaseClass对象可能会被其派生类的实例调用.我需要创建this与方法中相同类型的另一个实例.为每个派生类重载Method是不切实际的,因为它相当复杂,并且更有效地保持单个实现.
public class BaseClass
{
//constructors + properties + methods etc
public SomeMethod()
{
//some code
DerivedClass d = new DerivedClass(); //ideally determine the DerivedClass type at run-time
}
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了一些关于反射或使用动态关键字但我没有这些经验.
我正在阅读.NET 2.0书籍并遇到了这个获取应用程序程序集描述的示例代码:
static void Main(string[] args)
{
Assembly assembly = Assembly.GetExecutingAssembly();
object[] attributes =
assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
if (attributes.Length > 0)
{
AssemblyDescriptionAttribute descriptionAttribute =
(AssemblyDescriptionAttribute)attributes[0];
Console.WriteLine(descriptionAttribute.Description);
}
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
简单地获取程序集描述是相当多的代码,我想知道是否有更简单的方法在.NET 3.5+中使用LINQ或lambda表达式执行此操作?
我创建了一个小应用程序来递归加载提供的目录中的程序集并读取它们的自定义属性集合.主要是阅读DebuggableAttribute来确定IsJITTrackingEnabled和IsJITOptimizerDisabled的设置,以确定程序集是否针对发布进行了优化.
我当前的代码执行Assembly.LoadFrom将整个路径传递给程序集并加载它.然后在程序集上执行GetCustomAttributes以获取debuggable属性.问题是每个程序集都加载到当前的appdomain中.因此,如果另一个文件夹使用相同的程序集,它只使用最初加载的引用.我希望能够加载程序集,读取我需要的属性,然后卸载它.我尝试创建一个新的appdomain并将程序集加载到其中,然后卸载程序集后跟无济于事.
我知道这一定是可能的,但我不知所措.任何帮助将不胜感激.我很乐意提供您可能需要的任何其他信息.
我通过Reflection调查了2个接口和2个类:
对我来说奇怪的是,当我通过对IChild类型的反思时,我找不到IParent方法.
应用于Child类型的相同代码按预期工作 - 反射显示Parent方法.
interface IParent
{
void ParentMethod();
}
interface IChild : IParent
{
void ChildMethod();
}
class Parent
{
public void ParentMethod(){}
}
class Child : Parent
{
public void ChildMethod(){}
}
void Main()
{
//investigate derived interface
Type t = typeof(IChild);
var info = t.GetMethod("ChildMethod");//ok
Console.WriteLine(info);
info = t.GetMethod("ParentMethod");//returns null!
Console.WriteLine(info);
//investigate derived class
t = typeof(Child);
info = t.GetMethod("ChildMethod");//ok
Console.WriteLine(info);
info = t.GetMethod("ParentMethod");//ok
Console.WriteLine(info);
}
Run Code Online (Sandbox Code Playgroud)
请解释一下这种行为?
是否有任何解决方法可以从派生接口的类型中反映基接口的方法?
我试图动态加载加密程序集,但我GetType返回null,即使我使用正确的类名.这是代码:
//Load encryption assembly.
Assembly encryptionAssembly = Assembly.LoadFrom("Encryption.dll");
foreach(Type t in encryptionAssembly.GetTypes())
{
MessageBox.Show(t.Name.ToString());
// This shows "Encryption"
}
Type encryptionClass = encryptionAssembly.GetType("Encryption");
// But this returns null
Run Code Online (Sandbox Code Playgroud)
我对这个有点头疼.这堂课是公开的,我肯定拼写正确.
提前致谢.
我正在为我正在教授的课程实施一个自动"评估员".总的想法是每个学生都提供了一些DLL,并实现了一些算法.我的评估者使用Reflection加载所有这些DLL,找到学生实现并在锦标赛中评估它们.所有这些算法都是黑盒优化器,它们实现了以下接口
public interface IContinuousMetaheuristic
{
// ... Some unimportant properties
Vector Evaluate(Function function, int maxEvaluations, ...);
}
Run Code Online (Sandbox Code Playgroud)
Function(至少相关部分)的类定义是:
public class Function:
{
private Vector xopt; // The optimum point
private double fopt; // The optimum value
public double Evaluate(Vector x);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我需要将一个Function实例传递给这些元启发式.这些功能由我自己实现.其中大多数在某种意义上是随机的,也就是说,我在函数构造函数中选择一个随机最优点.这就是为什么你可以xopt在课堂上看到一个字段.问题是,我不希望我的学生能够通过反射或任何其他技术访问xopt或fopt领域,因为这将是作弊,或至少,如果他们这样做,所以我可以相应地惩罚他们;) .
因此,一般的问题是:是否有任何方法可以禁止在我动态加载的代码中使用Reflection,或者在任何其他意义上禁止此代码访问私有字段(作弊).
提前致谢.
我正在尝试将当前的类名称转换为字符串.
例如:
public class Marker : Mark
{
string currentclass = ???;
}
public abstract class MiniMarker : Mark
{
}
Run Code Online (Sandbox Code Playgroud)
我想从Marker类中获取字符串,所以我不必将它放在我从中创建的每个抽象类中.
我想要字符串MiniMarker,或者抽象类的名称.
我试过了MethodBase.GetCurrentMethod().DeclaringType,但没办法.
我想知道是否有任何方法可以确定给定java.lang.Method对象所代表的方法是否会覆盖另一个对象所代表的另一种方法java.lang.Method?
我正在研究Stronlgy类型的javascript,我需要能够知道一个方法是否覆盖了另一个方法,以便能够将它们重命名为更短的名称.
在这种情况下,我正在讨论覆盖的扩展定义,如@Override注释所支持,其中包括接口和抽象类方法的实现.
我对任何涉及直接反射或使用已经执行此操作的库的解决方案感到满意.
reflection ×10
c# ×8
inheritance ×2
java ×2
.net ×1
.net-4.0 ×1
abstract ×1
c#-4.0 ×1
casting ×1
class ×1
dynamic ×1
enums ×1
interface ×1
lambda ×1
linq ×1
overriding ×1
performance ×1
runtime ×1