小编aco*_*ner的帖子

在C#中动态创建Type数组

在C#中,我需要能够在运行时基于逗号分隔的数据类型列表创建一个Type对象数组,这些数据类型作为字符串传递给函数.基本上,这是我想要完成的:

// create array of types
Type[] paramTypes = { typeof(uint), typeof(string), typeof(string), typeof(uint) };
Run Code Online (Sandbox Code Playgroud)

但我需要能够像这样调用我的函数:

MyFunction("uint, string, string, uint");
Run Code Online (Sandbox Code Playgroud)

并让它根据传入的字符串动态生成数组.这是我的第一次尝试:

void MyFunction(string dataTypes)
{
    //out or in parameters of your function.   
    char[] charSeparators = new char[] {',', ' '};
    string[] types = dataTypes.Split(charSeparators,
                        stringSplitOptions.RemoveEmptyEntries);

    // create a list of data types for each argument
    List<Type> listTypes = new List<Type>();
    foreach (string t in types)
    {
        listTypes.Add(Type.GetType(t)); 
    }
    // convert the list to an array
    Type [] paramTypes = listTypes.ToArray<Type>(); …
Run Code Online (Sandbox Code Playgroud)

c# arrays runtime list arraylist

8
推荐指数
2
解决办法
2万
查看次数

结合MVVM Light Toolkit和Unity 2.0

这更像是一个评论而不是一个问题,尽管反馈会很好.我的任务是为我们正在做的新项目创建用户界面.我们想使用WPF,我想学习所有现代UI设计技术.由于我是WPF的新手,我一直在研究可用的东西.我想我已经基本上决定使用MVVM Light Toolkit(主要是因为它的"可混合性"和EventToCommand行为!),但我也希望合并IoC.所以,这就是我想出的.我修改了MVVM Light项目中的默认ViewModelLocator类,以使用UnityContainer来处理依赖注入.考虑到我不知道这些术语的90%在3个月前是什么意思,我认为我走在了正确的轨道上.

// Example of MVVM Light Toolkit ViewModelLocator class that implements Microsoft 
// Unity 2.0 Inversion of Control container to resolve ViewModel dependencies.

using Microsoft.Practices.Unity;

namespace MVVMLightUnityExample
{
    public class ViewModelLocator
    {
        public static UnityContainer Container { get; set; }

        #region Constructors
        static ViewModelLocator() 
        {
            if (Container == null)
            {
                Container = new UnityContainer();

                // register all dependencies required by view models
                Container
                    .RegisterType<IDialogService, ModalDialogService>(new ContainerControlledLifetimeManager())
                    .RegisterType<ILoggerService, LogFileService>(new ContainerControlledLifetimeManager())
                    ;
            }
        }

        /// <summary>
        /// Initializes a …
Run Code Online (Sandbox Code Playgroud)

wpf ioc-container unity-container mvvm-light

7
推荐指数
1
解决办法
3170
查看次数