问题描述
亲爱的所有人
我是C#的初学者,并且正在与以下人员作斗争.
我尝试设计一个通用类,在该类中,方法应将值分配给参数.对于基本数据类型,使用ref或out参数很容易.但是我喜欢从另一个定义get/set方法的类传递一个属性.
问题是,我不知道是否有可能,如果可以,怎么办:(
提前谢谢您.
问候
代码片段:
a.)通用类
Dear all
I’m a beginner in c# and I’m fighting with the following.
I try to design a generic class, in which a method should assign a value to a parameter. For basic data types it is easy with a ref or out parameter. But I like to pass a property from another class which defines the get/set method.
The problem is, I do not have any idea whether it is possible and if yes how to do it :(
Thank you in advance.
Regards
Code fragments:
a.) The Generic class
public class MyGeneric<TWhatever, TResult>
{
//
// ******Here you find my problem******
//
// This method should assign the result to a property of another class
public bool TryDoAnything(???here I like to pass "MyAny.AnyData" to assign
data as parameter result???)
{
...
if (success)
{
// In real the result comes from TWhatever
result= new TResult();
}
return(success);
}
}
b.)在应用程序中
b.) In the application
{
MyGeneric<TheWhatever, PropertyClass> myGenericInUse=
new MyGeneric<TheWhatever, PropertyClass>();
MainClass myMain= new MainClass();
...
...
//
// ******Here probably a have the next problem******
//
if (myGenericInUse.TryDoAnything(myMain.PropData))
{
// Do what is needed
}
}
c.)具有必须由通用类设置的属性的类
c.) The class with the property which has to be set by the generic class
public class MainClass
{
private PropertyClass propData;
public PropertyClass PropData
{
get
{
return(propData);
}
set
{
propData= value;
}
}
}
d.)结果类(仅用于完整性)
d.) The result class (only for completness)
public class PropertyClass
{
}
推荐答案
class Program
{
static void Main(string[] args)
{
int i = 10;
GenericExample<int> ge = new GenericExample<int>();
Console.WriteLine("The value of i is " + i.ToString());
ge.SetSomeValue(ref i);
Console.WriteLine("The value of i is " + i.ToString());
Console.ReadKey();
}
}
public class GenericExample<TResult>
{
public bool SetSomeValue(ref TResult value)
{
value = Activator.CreateInstance<TResult>();
return true;
}
}
在控制台应用程序中运行此代码时,您会看到i
在执行该函数之前为10,在执行该函数之后为0.您可以将i
设置为所需的任何Property
的值.
但是,为什么需要返回bool
表示成功?为什么不简单地返回TResult
的实例并在出现问题时抛出Exception
(它减少了if-blocks
的数量并允许适当的错误处理)?
如果您确实想设置Property
(即在Property
上调用set方法并传递在方法中以某种方式计算出的值),则可以使用Reflection
,但我不建议这样做.这是您的操作方法.
When running this code in a console application you will see that i
is 10 before executing the function and 0 after executing it. You can set i
as the value of whatever Property
you want.
However, why do you need to return a bool
to indicate success? Why not simply return an instance of TResult
and throw an Exception
if something went wrong (it reduces the amount of if-blocks
and allows for proper error handling)?
If you really wanted to set a Property
(that is, invoke the set method on a Property
and passing the value that was somehow calculated in your method) you could use Reflection
, but I don''t recommend it. Here is how you would do it.
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.SomeProperty = 24;
GenericExample<int> ge = new GenericExample<int>();
Console.WriteLine("The value of SomeProperty is " + p.SomeProperty.ToString());
ge.SetProperty(p, "SomeProperty");
Console.WriteLine("The value of SomeProperty is " + p.SomeProperty.ToString());
Console.ReadKey();
}
}
public class Person { public int SomeProperty {get; set;} }
public class GenericExample<TRresult>
{
public bool SetProperty(object obj, string propertyName)
{
System.Reflection.PropertyInfo prop = obj.GetType().GetProperty(propertyName);
prop.GetSetMethod().Invoke(obj, new object[] {Activator.CreateInstance<TResult>()});
return true;
}
}
您可以看到它的缺点.如果您更改SomeProperty
的名称或拼写错误,则不会为错误提供设计时间支持.
You can see the downsides to this. If you would change the name of SomeProperty
or misspell it you wouldn''t get design time support for the error.
public class MyGeneric<TWhatever,TResult>
{
// Define an event type
public delegate void PropertyAssign(TResult Value);
// Pass a delegate (event?) to assign the result
public bool TryDoAnything(PropertyAssign assignResult)
{
...
if (success)
{
// In real the result comes from TWhatever
assignResult(new TResult());
}
return(success);
}
}
b.)在应用程序中使用它
b.) Use it in the application
{
MyGeneric<TheWhatever, MainClass> myGenericInUse= new
MyGeneric<TheWhatever, MainClass>();
MainClass myMain= new MainClass();
...
...
//
// Here how to use it. A lot to write....
//
if (myGenericInUse.TryDoAnything(delegate(PropertyClass value)
{myMain.PropData= value;}))
{
// Do what is needed
}
}
这篇关于是否可以将属性作为方法参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!