我有一个实现接口的对象列表,以及该接口的列表:
public interface IAM
{
int ID { get; set; }
void Save();
}
public class concreteIAM : IAM
{
public int ID { get; set; }
internal void Save(){
//save the object
}
//other staff for this particular class
}
public class MyList : List<IAM>
{
public void Save()
{
foreach (IAM iam in this)
{
iam.Save();
}
}
//other staff for this particular class
}
Run Code Online (Sandbox Code Playgroud)
之前的代码无法编译,因为编译器要求所有接口成员都是公共的.
internal void Save(){
Run Code Online (Sandbox Code Playgroud)
但我不想让我的DLL外部保存ConcreteIAM,它只应该通过保存MyList.
有什么办法吗?
更新#1:大家好,感谢目前为止的答案,但这些都不是我需要的:
接口需要是公共的,因为它是来自dll外部的客户端将使用的签名,以及 …
我有一个下拉列表,通过检查类的方法并包括与特定签名匹配的方法来填充.问题在于从列表中获取所选项目并让委托在类中调用该方法.第一种方法有效,但我无法弄清楚第二种方法的一部分.
例如,
public delegate void MyDelegate(MyState state);
public static MyDelegate GetMyDelegateFromString(string methodName)
{
switch (methodName)
{
case "CallMethodOne":
return MyFunctionsClass.CallMethodOne;
case "CallMethodTwo":
return MyFunctionsClass.CallMethodTwo;
default:
return MyFunctionsClass.CallMethodOne;
}
}
public static MyDelegate GetMyDelegateFromStringReflection(string methodName)
{
MyDelegate function = MyFunctionsClass.CallMethodOne;
Type inf = typeof(MyFunctionsClass);
foreach (var method in inf.GetMethods())
{
if (method.Name == methodName)
{
//function = method;
//how do I get the function to call?
}
}
return function;
}
Run Code Online (Sandbox Code Playgroud)
如何使第二种方法的注释部分工作?我如何MethodInfo投入代表?
谢谢!
编辑:这是工作解决方案.
public static MyDelegate GetMyDelegateFromStringReflection(string methodName) …Run Code Online (Sandbox Code Playgroud) 我有
public enum Als
{
[StringValue("Beantwoord")] Beantwoord = 0,
[StringValue("Niet beantwoord")] NietBeantwoord = 1,
[StringValue("Geselecteerd")] Geselecteerd = 2,
[StringValue("Niet geselecteerd")] NietGeselecteerd = 3,
}
Run Code Online (Sandbox Code Playgroud)
同
public class StringValueAttribute : Attribute
{
private string _value;
public StringValueAttribute(string value)
{
_value = value;
}
public string Value
{
get { return _value; }
}
}
Run Code Online (Sandbox Code Playgroud)
我想将我从组合框中选择的项的值放入int:
int i = (int)(Als)Enum.Parse(typeof(Als), (string)cboAls.SelectedValue); //<- WRONG
Run Code Online (Sandbox Code Playgroud)
这是可能的,如果是的话,怎么样?(StringValue匹配从组合框中选择的值).
如何获取MemberInfo对象的值?.Name返回变量的名称,但我需要该值.
我认为你可以这样做,FieldInfo但我没有一个片段,如果你知道如何做到这一点你能提供一个片段吗?
谢谢!
我有一个带out参数的方法,尝试进行类型转换.基本上:
public void GetParameterValue(out object destination)
{
object paramVal = "I want to return this. could be any type, not just string.";
destination = null; // default out param to null
destination = Convert.ChangeType(paramVal, destination.GetType());
}
Run Code Online (Sandbox Code Playgroud)
问题是,通常会有人称之为:
string output;
GetParameterValue(output);
Run Code Online (Sandbox Code Playgroud)
这将失败,因为:
destination.GetType()
Run Code Online (Sandbox Code Playgroud)
destination为null,因此我们无法调用.GetType()它.我们也不能打电话:
typeof(destination)
Run Code Online (Sandbox Code Playgroud)
因为destination是变量名而不是类型名.
那么有没有办法获得设置为null的对象的类型?我认为必须有一种方法可以知道什么类型的存储位置没有分配任何东西.
只是为了提供更多信息,我试图创建一个实用程序方法来获取Oracle存储过程的输出参数.问题是DbParameter.Value对象类型.
对于开发人员来说,最理想的是:
string val = GetParameterValue("parameterName");
Run Code Online (Sandbox Code Playgroud)
值得注意的是,没有类型的铸造.在实践中,你不知道"等于"的lparam,所以我选择了:
string val;
GetParameterValue("parameterName", out val);
Run Code Online (Sandbox Code Playgroud)
在方法中,我会知道输出变量的目标类型.我猜这是一个不好的假设.作为替代方案,我也写了这个方法:
public T GetParameterValue<T>(string paramName)
Run Code Online (Sandbox Code Playgroud)
所以开发人员可以这样做:
string val = GetParameterValue<string>("parameterName");
Run Code Online (Sandbox Code Playgroud)
我发现显式的"字符串"声明是重复的,特别是因为在实践中,目标可能是对象属性和oracle数据类型可能会改变(想想ORM):
MyObj.SomeProp = GetParameterValue<MyObj.SomeProp.GetType()>("parameterName");
Run Code Online (Sandbox Code Playgroud)
但同样,如果MyObj.SomeProp为null,则该.GetType()调用失败.VM必须知道它的类型MyObj.SomeProp …
我知道如何使用Action和Func在.NET中,但每次我开始,使用我调用的常规旧方法可以实现完全相同的解决方案.
这排除了当一个Action或者Func被用作我无法控制的东西的参数时,比如LINQ .Where.
基本上我的问题是......为什么这些存在?他们给了我什么额外的和新的一个简单的方法不?
我正在尝试在编译时反序列化一个未知类型的数组.在运行时我发现了类型,但我不知道如何创建实例.
就像是:
Object o = Activator.CreateInstance(type);
Run Code Online (Sandbox Code Playgroud)
这是行不通的,因为没有无参数构造函数,Array似乎没有任何构造函数.
在C#中,您可以匿名定义委托(即使它们只不过是语法糖).例如,我可以这样做:
public string DoSomething(Func<string, string> someDelegate)
{
// Do something involving someDelegate(string s)
}
DoSomething(delegate(string s){ return s += "asd"; });
DoSomething(delegate(string s){ return s.Reverse(); });
Run Code Online (Sandbox Code Playgroud)
是否可以在Java中传递这样的代码?我正在使用处理框架,它有一个相当旧版本的Java(它没有泛型).
System.Activator.CreateInstance(T)方法是否有性能问题(因为我怀疑它使用反射)大到足以阻止我们随便使用它?
EnumerableObject : IEnumerable<Foo>
包裹一个 List<Foo>
如果EnumerableObject a.SequenceEquals( EnumerableObject b),那么他们是平等的.
因此,GetHashCode必须实施.问题是XORing列表中的每个元素将返回任何列表的相同哈希码,所有列表都包含所有且只有相同的元素,而不管顺序如何.就工作而言,这是好的,但会导致许多冲突,这将减慢检索速度等.
GetHashCode对于依赖于顺序的对象列表,什么是一种好的,快速的方法?
c# ×9
.net ×4
reflection ×3
delegates ×2
.net-2.0 ×1
action ×1
activator ×1
algorithm ×1
attributes ×1
enums ×1
func ×1
function ×1
gethashcode ×1
gettype ×1
hashcode ×1
interface ×1
java ×1
performance ×1
processing ×1
types ×1